异步爬取小说所有章节并下载

下载百度APP里面的西游记

1.查找获取所有章节标题的url:

https://dushu.baidu.com/api/pc/getCatalog?data={"book_id":"4306063500"}

进入第一章节,详细内容的url:

https://dushu.baidu.com/api/pc/getChapterContent?data={"book_id":"4306063500","cid":"4306063500|1569782244","need_bookinfo":1}

2.访问getCatalog 拿到所有章节的cid和名称,准备异步下载任务

async def getCatalog(url):
    resp = requests.get(url)
    dic = resp.json()
    tasks = []
    for item in dic['data']['novel']['items']:  # item对应每一个章节的名称和cid
        title = item['title']
        cid = item['cid']
        # 准备异步下载任务
        p = asyncio.create_task(aiodownload(cid, b_id, title))
        tasks.append(p)
    await asyncio.wait(tasks)

3.访问getChapterContent 异步下载所有章节的内容

async def aiodownload(cid, b_id, title):
    data={
        "book_id": f"{b_id}",
        "cid": f"{b_id}|{cid}",
        "need_bookinfo": 1
    }

    data = json.dumps(data)
    url = f"https://dushu.baidu.com/api/pc/getChapterContent?data={data}"

    async with aiohttp.ClientSession() as session:
        async with session.get(url) as resp:
            dic = await resp.json()

            async with aiofiles.open("西游记/"+title, 'w', encoding="utf-8") as f:
                await f.write(dic['data']['novel']['content'])  # 把小说内容写出

4.main函数

if __name__ == '__main__':
    b_id = "4306063500"
    url = 'https://dushu.baidu.com/api/pc/getCatalog?data={"book_id":"' + b_id + '"}'
    asyncio.run(getCatalog(url))

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值