2k小说网爬取最近大火的《东宫》小说,借鉴之前看过的一段代码,修改之后,进行简单爬取。
from urllib import request
from bs4 import BeautifulSoup
url='https://www.fpzw.com/xiaoshuo/19/19210/'
req=request.Request(url)
response=request.urlopen(req)
html=response.read()
soup=BeautifulSoup(html,'html.parser')
soup_text=soup.find_all('dd')[4:]
f= open('Desktop/donggong.doc','w',encoding='utf-8')
for link in soup_text:
url2='https://www.fpzw.com/xiaoshuo/19/19210/'+link.a.get('href')
req2=request.Request(url2)
response2=request.urlopen(req2)
html2=response2.read()
soup2=BeautifulSoup(html2,'html.parser')
soup_text2=soup2.find('p',class_="Text").text
soup_text3=soup_text2.replace('东宫最新章节','')
soup_text3=soup_text3.replace('2k小说网欢迎您!本站域名:"2k小说"的完整拼音fpzw.com,很好记哦!www.fpzw.com 好看的小说','')
soup_text3=soup_text3.replace('强烈推荐:','')
f.write(soup_text3)
f.write('
')
f.close()
爬取的结果没进行精细处理,后续待优化。