迷失的小屋
首页
注册

[Request]2025/4/18爬取百度小说《西游记》

迷失的蒙娜丽莎
迷失的蒙娜丽莎
2025-04-18 11:35:10

import requests

import time



resp=requests.get('https://dushu.baidu.com/api/pc/getCatalog?data={%22book_id%22:%224306063500%22}')

dic_data=resp.json()

item_list=dic_data['data']['novel']['items']

url='https://dushu.baidu.com/api/pc/getChapterContent?data={"book_id":"4306063500","cid":"4306063500|'

for index,item in enumerate(item_list):

cid=item['cid']

baseUrl=url+cid+'"}'

resp1=requests.get(baseUrl)

novel_text=resp1.json()

novel=novel_text['data']['novel']['content']

title=item['title']

print(novel)

with open('novel/'+title+'.txt','w',encoding='utf-8') as novel_writer:

novel_writer.write(novel)

time.sleep(5)