爬取吞噬星空小说
作者:互联网
import requests
import re
url = "https://www.777zw.net/2/2142/751765.html"
with open('tunshixingkong.txt', 'a', encoding='utf8') as f:
for i in range(5000):
try:
r = requests.get(url)
r.encoding = 'gbk'
url = re.findall('<a href="(.*?)">下一章</a>', r.text)[0]
url = url.split(';')[-1]
url = re.findall('<a href="(.*?).html', url)[0]
url = f"https://www.777zw.net/2/2142/{url}.html"
title = re.findall('<h1>(.*?)</h1>', r.text)[0]
content = re.findall('id="content">(.*?)</div>', r.text)[0].strip(' ').replace("<br /><br /> ","\n")
f.write(title+'\n'+content+'\n'*2)
print(f'第{i+1}章')
except:
print('defeat!')
print(url)
标签:content,星空,url,text,爬取,re,吞噬,nbsp,findall 来源: https://www.cnblogs.com/xzztj/p/15861938.html