用户:
野生研究员查看:0 回复:1 评论:0 创建时间:2022-12-23T15:52:20
我最近想爬编程猫的图书馆
发现json文件url为https://api.codemao.cn/api/fanfic/section/51118(类似这种,最后是文章id)
然后想for动态生成最后数字
结果爬出来跟们不是一个小说...
谁能帮一下我
import requests
from bs4 import BeautifulSoup
import json
def get_text(url):
try:
req = requests.get(url=url)
html = json.loads(req.text)
title = html['data']['section']['title']
html = html['data']['section']['content']
message = {
'html':html,
'title':title,
}
except KeyError:
return False
else:
return message
def witer(text):
with open('bianchengmao.txt','a',encoding='utf-8') as f:
f.writelines(text)
f.write('\n')
if __name__ == '__main__':
for x in range(1, 98359):
message = get_text(f'https://api.codemao.cn/api/fanfic/section/{x}')
if message:
bf = BeautifulSoup(message['html'])
text = bf.find_all('p')
for n in range(0,len(text)):
e = text[n].text.replace('\xa0'*4, '\n\n')
witer(e)
print(f'正在爬取:https://api.codemao.cn/api/fanfic/section/{x}')
print(f'文章序号:{x}\n')