萌萌的烤肥宅 2021-06-12 17:44 采纳率: 100%
浏览 85
已采纳

关于爬取小说却不显示

代码:

import re

import  requests

import  parsel

headers = {

        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36'

    }

def namp(url):#打印一章小说

    # url = requests.get('https://www.shuquge.com/txt/54528/8318127.html', headers=headers)

    repost = requests.get(url,headers=headers)

    repost.encoding = repost.apparent_encoding

 

    html1 = repost.text

    sel1 = parsel.Selector(html1)

 

    coetnen = sel1.css('.content h1::text').get()

    coetnen1 = sel1.css('#content::text').getall()

    print(coetnen)

 

    moeqi = [i.strip() for i in coetnen1]

    texr = '\n'.join(moeqi)

    print(texr)

 

    with open('D:/getup/无限恐怖.txt', mode='a+', encoding='utf-8') as f:

        f.write(texr)

        f.write(coetnen)

 

def get_niam(book_url):#传入小说目录

    lsit = requests.get(book_url)

    book_url.encoding = book_url.apparent_encoding

    html = book_url.text

    # print(html)

    sel = parsel.Selector(html)

    lusop = sel.css('dd a::attr(href)').getall()

    return lusop

def huop(book_url):

    lost = get_niam(book_url)

    for losts in lost:

        print('https://www.shuquge.com/txt/54528/' + losts)

        namp('https://www.shuquge.com/txt/54528/' + losts)

if __name__ == '__main__':

    book_url = 'https://www.shuquge.com/txt/54528/index.html'

    print(book_url)

可以输出链接但是
为什么def里面的

print()没有打印出结果

  • 写回答

1条回答 默认 最新

  • 江天暮雪丨 2021-06-12 17:52
    关注

    首先,你用def定义了函数,但是你没调用,自然就不出打印结果。

    然后是这段代码

    def get_niam(book_url):#传入小说目录
    
        lsit = requests.get(book_url)
    
        book_url.encoding = book_url.apparent_encoding
    
        html = book_url.text
     
    ##
    
    def get_niam(book_url):  # 传入小说目录
    
        lsit = requests.get(book_url)
    
        lsit.encoding = lsit.apparent_encoding
    
        html = lsit.text
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥15 统计大规模图中的完全子图问题
  • ¥15 使用LM2596制作降压电路,一个能运行,一个不能
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错
  • ¥20 @microsoft/fetch-event-source 流式响应问题