这是小白用来练手的~
欢迎指出不足!!
用的是python 3.7
import requests
import re
##1.确定爬取的网址
url='http://www.biquge.info/10_10582/'
##2发送请求
response=requests.get(url=url)
response.encoding='utf-8'
html_data=response.text
##3.数据解析
result_list=re.findall('<dd><a href="(.*?)" title="(.*?)">.*</a></dd>',html_data)
##()表示精确匹配,.表示除了换行符以外的任意字符,表示贪婪匹配,*表示匹配前一个字符0次或者多次,?表示非贪婪模式
for chapter_url,title in result_list:
try:
all_url='http://www.biquge.info/10_10582/'+chapter_url
response_2=requests.get(url=all_url)
response_2.encoding='utf-8'
html_data_2=response_2.text
## 解析章节小说文本
result=re.findall('<div id="content">(.*?)</div>',html_data_2,re.S)##删掉<!--go-->
if result:
## text=result[0].replace(' ',"").replace('<br/>',"\n")
##4.保存数据
with open('三寸人间\\'+title+'.txt',mode='w',encoding='utf-8')as f:
f.write(result[0].replace(' ',"").replace('<br/>',"\n").replace('<!--go-->',""))
print('保存成功:',title)
except Exception as e:
print(e)
关于Python中No such file or directory报错解决办法
这里有一篇文章参考
https://zhuanlan.zhihu.com/p/141640843?from_voters_page=true