m0_56302292 2023-12-11 11:15 采纳率: 76.5%
浏览 7
已结题

爬取小说动态章节内容

如何爬取小说全部章节内容,可以帮忙修改一下吗


import requests
from bs4 import BeautifulSoup

def content(url):
    target = url
    response = requests.get(target)
    response.encoding = 'utf-8'
    html = response.text
    bf = BeautifulSoup(html,"lxml")
    texts = bf.find("div",id = "allCatalog")
    return texts.text


if __name__=='__main__':
    target = 'https://www.qidian.com/book/1038368163.html'
    response = requests.get(target)
    response.encoding = 'utf-8'
    html = response.text
    bf = BeautifulSoup(html,"lxml")
    texts = bf.find("div",id = "allCatalog")
    texts = texts.find_all("a")
    sum = 0
    names = []
    urls = []
    for i in texts:
        names.append(i.string)
        urls.append(i.get("href"))
    for i in range(len(names)):
        url = 'https://www.qidian.com'+urls[i]
        word = content(url)
        with open('从杂鱼开始刷经验修仙/'+str(names[i])+'.txt',"a",encoding='utf-8')as f:
            f.write(word)
    print("下载成功")
  • 写回答

1条回答 默认 最新

  • 7*24 工作者 2023-12-11 13:02
    关注
    
    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    # pip install lxml,requests
    
    import requests
    from lxml import etree
    from time import sleep
    from os.path import isfile
    
    start_url = 'https://www.qidian.com/book/1038368163.html'
    headers = {
        'Host': 'www.qidian.com',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36'
    }
    
    # result = {'第一章 杂鱼 分筋错骨': 'https://www.qidian.com/chapter/1038368163/771982316/',
    #           '第二章 有志者 事竟成': 'https://www.qidian.com/chapter/1038368163/772038871/',
    #           '第三章 水鬼 谣言': 'https://www.qidian.com/chapter/1038368163/772103342/',
    #           '第四章 浮尸': 'https://www.qidian.com/chapter/1038368163/772172852/',
    #           '第五章 夜变': 'https://www.qidian.com/chapter/1038368163/772218059/',
    #           '第六章 杀人 补刀': 'https://www.qidian.com/chapter/1038368163/772248694/',
    #           '第七章 这事 要命': 'https://www.qidian.com/chapter/1038368163/772286450/',
    #           '第八章 不疯魔 不成活': 'https://www.qidian.com/chapter/1038368163/772308085/',
    #           '第九章 毙敌 抛尸': 'https://www.qidian.com/chapter/1038368163/772358375/',
    #           '第十章 是个高手': 'https://www.qidian.com/chapter/1038368163/772410842/',
    #           '第十一章 这世道 也得看银子说话': 'https://www.qidian.com/chapter/1038368163/772468204/',
    #           '第十二章 精神病 鬼上身': 'https://www.qidian.com/chapter/1038368163/772513442/',
    #           '第十三章 自焚': 'https://www.qidian.com/chapter/1038368163/772560735/',
    #           '第十四章 一个小目标': 'https://www.qidian.com/chapter/1038368163/772622699/',
    #           '第十五章 多多益善': 'https://www.qidian.com/chapter/1038368163/772660480/',
    #           '第十六章 风雪夜寻人': 'https://www.qidian.com/chapter/1038368163/772714284/',
    #           '第十七章 当头棒 穿心箭': 'https://www.qidian.com/chapter/1038368163/772763113/',
    #           '第十八章 道一声公子': 'https://www.qidian.com/chapter/1038368163/772835192/',
    #           '第十九章 劲如流水 难畅通': 'https://www.qidian.com/chapter/1038368163/772874185/',
    #           '第二十章 熟能生巧 触类旁通': 'https://www.qidian.com/chapter/1038368163/772899101/',
    #           '第二十一章 略有小成': 'https://www.qidian.com/chapter/1038368163/772946279/',
    #           '第二十二章 错骨断铁砂': 'https://www.qidian.com/chapter/1038368163/772967660/',
    #           '第二十三章 万万没想到': 'https://www.qidian.com/chapter/1038368163/773020834/',
    #           '第二十四章 遇事不决 铁锅炖鱼': 'https://www.qidian.com/chapter/1038368163/773060799/',
    #           '第二十五章 一身汗 千斤墩': 'https://www.qidian.com/chapter/1038368163/773118633/',
    #           '第二十六章 山妖': 'https://www.qidian.com/chapter/1038368163/773165283/',
    #           '第二十七章 人头祭': 'https://www.qidian.com/chapter/1038368163/773225276/',
    #           '第二十八章 一点粮 三条命': 'https://www.qidian.com/chapter/1038368163/773264525/',
    #           '第二十九章 一声春雷响 一炁万物生': 'https://www.qidian.com/chapter/1038368163/773328248/',
    #           '第三十章 风光': 'https://www.qidian.com/chapter/1038368163/773455194/',
    #           '第三十一章 我低调不了啊': 'https://www.qidian.com/chapter/1038368163/773599787/',
    #           '第三十二章 面子 绑了': 'https://www.qidian.com/chapter/1038368163/773697345/',
    #           '第三十三章 不出意外的出了意外': 'https://www.qidian.com/chapter/1038368163/773756851/',
    #           '第三十四章 怕什么来什么': 'https://www.qidian.com/chapter/1038368163/773769599/',
    #           '第三十五章 直面山妖': 'https://www.qidian.com/chapter/1038368163/773801585/',
    #           '第三十六章 浮尸再现': 'https://www.qidian.com/chapter/1038368163/773828794/',
    #           '第三十七章 大雨浮棺': 'https://www.qidian.com/chapter/1038368163/773881448/',
    #           '第三十八章 御水': 'https://www.qidian.com/chapter/1038368163/774065826/',
    #           '第三十九章 杀城': 'https://www.qidian.com/chapter/1038368163/774149539/',
    #           '第四十章 夜来风雨声': 'https://www.qidian.com/chapter/1038368163/774244360/',
    #           '第四十一章 双杀': 'https://www.qidian.com/chapter/1038368163/774294602/',
    #           '第四十二章 猛龙过江': 'https://www.qidian.com/chapter/1038368163/774359812/',
    #           '第四十三章 开疆拓土': 'https://www.qidian.com/chapter/1038368163/774401011/',
    #           '第四十四章 三年打熬 两年觉炁': 'https://www.qidian.com/chapter/1038368163/774426816/',
    #           '第四十五章 我信你个鬼': 'https://www.qidian.com/chapter/1038368163/774484083/',
    #           '第四十六章 这么豪横吗': 'https://www.qidian.com/chapter/1038368163/774530105/',
    #           '第四十七章 鬼鬼祟祟': 'https://www.qidian.com/chapter/1038368163/774595391/',
    #           '第四十八章 水府': 'https://www.qidian.com/chapter/1038368163/774635110/',
    #           '第四十九章 河伯': 'https://www.qidian.com/chapter/1038368163/774667079/',
    #           '第五十章 巡江使': 'https://www.qidian.com/chapter/1038368163/774714366/',
    #           '第五十一章 破庙 吓人': 'https://www.qidian.com/chapter/1038368163/774772920/',
    #           '第五十二章 河车搬运 打架很猛': 'https://www.qidian.com/chapter/1038368163/774876724/',
    #           '第五十三章 八方来财': 'https://www.qidian.com/chapter/1038368163/774947704/',
    #           '第五十四章 废物 说谁呢': 'https://www.qidian.com/chapter/1038368163/774995116/',
    #           '第五十五章 罗汉功': 'https://www.qidian.com/chapter/1038368163/775101728/',
    #           '第五十六章 这个就叫专业': 'https://www.qidian.com/chapter/1038368163/775238653/',
    #           '第五十七章 人为财死': 'https://www.qidian.com/chapter/1038368163/775295308/',
    #           '第五十八章 尸变': 'https://www.qidian.com/chapter/1038368163/775360561/',
    #           '第五十九章 尸体也疯狂': 'https://www.qidian.com/chapter/1038368163/775451984/',
    #           '第六十章 百年旧事': 'https://www.qidian.com/chapter/1038368163/775515382/',
    #           '第六十一章 老人家 请自重': 'https://www.qidian.com/chapter/1038368163/775526606/',
    #           '第六十二章 好生欠揍': 'https://www.qidian.com/chapter/1038368163/775572076/',
    #           '第六十三章 二百岁': 'https://www.qidian.com/chapter/1038368163/775596799/'}
    
    
    def getLevelAddr(start_url = 'https://www.qidian.com/book/1038368163.html'):
        '''获取页面中所有的章节链接'''
        result = {}
    
        response = requests.get( start_url,headers=headers ).content.decode('utf-8')
    
        ## 直接使用 xpath 解析,获取数据
        html = etree.HTML(response)
        lis = html.xpath('//div[@id="allCatalog"]//ul[@class="volume-chapters"]//a[@class="chapter-name"]')
        for i in lis:
            link = 'https:' + i.xpath( './@href' )[0]
            name = i.xpath('./text()')[0].strip()
            result[name] = link
        return result
    
    def singlePageSave(result):
        for key,url in result.items():
            response = requests.get(url, headers=headers).content.decode('utf-8')
            html = etree.HTML(response)
            content = html.xpath('//div[@class="relative"]//main[@id="c-771982316"]/p/text()')
            # print(content)
            if not isfile( f'{key}.txt'  ):
                with open(f'{key}.txt','w',encoding='utf-8') as wf:
                    for item in content:
                        wf.write(item.strip() + '\n')
            # break
    def main():
        result = getLevelAddr()
        # 添加sleep 主要是为了防止爬取速度过快,而导致IP被封掉
        sleep(1)
        if result:
            singlePageSave(result)
            sleep(1)
    
    
    
    
    if __name__ == '__main__':
        main()
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 12月22日
  • 已采纳回答 12月14日
  • 创建了问题 12月11日