python多线程爬取某度小说

本文介绍了如何使用Python编程语言,通过调用百度网盘API获取图书《万相之王》的章节内容,并利用异步IO库如aiohttp和asyncio实现批量下载。
摘要由CSDN通过智能技术生成

链接地址:https://dushu.baidu.com/pc/reader?gid=4356290733&cid=1569830905

环境配置:Python 3.8.6

python百度网盘链接:https://pan.baidu.com/s/1I9QkobmvsEAJIjX-8vilWw?pwd=qxdg 
提取码:qxdg 

工具:pycharm :PyCharm:JetBrains为专业开发者提供的Python IDE

pip 按照库

pip install aiofiles aiohttp requests asyncio

 安装好以后,f12打开网址,发现网址有所有章节的内容

然后点开第一章节,发现连接中包含第一章节内容

获取两个链接url:将%22 修改为"   [编码格式问题]

# https://dushu.baidu.com/api/pc/getCatalog?data={"book_id"":"4356290733"} => 所有章节的内容(名称,cid)
# https://dushu.baidu.com/api/pc/getChapterContent?data={"book_id":"4356290733","cid":"4356290733|1569830905","need_bookinfo":1}k开始
# https://dushu.baidu.com/api/pc/getCatalog?data={"book_id"":"4356290733"} => 所有章节的内容(名称,cid)
# https://dushu.baidu.com/api/pc/getChapterContent?data={"book_id":"4356290733","cid":"4356290733|1569830905","need_bookinfo":1}

import json
import os

import aiofiles
import aiohttp
import requests
import asyncio


async def downlod(cid, b_id, title):
    data = {
        'book_id': 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()
            folder_path = '万相之王'  # 定义文件夹路径,可以根据需要修改
            os.makedirs(folder_path, exist_ok=True)  # 创建文件夹,如果不存在的话

            file_path = os.path.join(folder_path, title)  # 构建文件路径
            async with aiofiles.open(file_path, mode="w", encoding="utf") as f:
                await f.write(dic['data']['novel']['content'])
            # data = dic['data']['novel']['content']
async def getCatlog(url):
    resp = requests.get(url)
    dic = resp.json()
    tasks = []
    for item in dic['data']['novel']['items']:
        title = item['title']
        cid = item['cid']

        tasks.append(downlod(cid, b_id, title))
    await asyncio.wait(tasks)


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

运行程序以后:发现同级目录创建了万相之王的文件夹,下载完成

  • 6
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值