正则表达式练习,仅供学习交流
import requests,re,os
url1='https://qbxs520.com/book/'
i=input("请输入书籍ID:")
url=(url1+i+'/')
req=requests.get(url)
name=re.findall('<h2>《(.*?)》',req.text)[0]
mulu=re.findall('.html">(.*?)</a>',req.text)[9:]
wangzhi=re.findall(f'href="/book/{i}/(.*?).html',req.text)
wangzhi=set(wangzhi)
wangzhi=list(wangzhi)
wangzhi.sort()
dict1={}
for i in range(len(mulu)):
dict1[mulu[i]]=f'{url}{wangzhi[i]}.html'
if not os.path.exists(f'E:/Desktop/{name}'):
os.mkdir(f'E:/Desktop/{name}')
count=1
for m,n in dict1.items():
if count<1:
break
else:
req = requests.get(n)
neirong = re.findall('id="booktext">(.*?)</p>', req.text, re.S)[0]
neirong = neirong.replace('<br />', '\n').replace(' ','')
with open(f'E:/Desktop/{name}/{m}.txt','w+') as file2:
file2.write(neirong)
print(f'{m}爬取完毕')
count+=1