python爬取小说
通过python爬取小说
目录地址 url = ‘https://www.shicimingju.com/book/sanguoyanyi.html’
在爬取小说的时候,我的IP被服务器冻结了一次,后来我用的代理爬取的,爬虫容易被封IP,各网站有其反扒机制,最常用的还是检测你的IP然后封IP,反爬机制我就不详说了。
分析我们需要的内容
我这边爬取的是三国演义,我们通过谷歌浏览器的F12查看Elements结构树
发现是在div标签下的book-mulu类里面,里面有ul,li,a标签,a标签是我们想要的
点进去发现他跳到的href连接是/books下面的,所以我们要爬取的是他的那个链接页面
我们发现每个章节的内容是在div类chapter_content里面的p标签下面
到现在,我们分析结构已经分析完了。具体怎么分析,可以看我之前的博客Python抓取糗事百科的搞笑图片
需要引入的库
requests,BeautifulSoup,UserAgent
import requests
from bs4 import BeautifulSoup
from fake_useragent import UserAgent
首先我们进行UA伪装,也是防止被封IP的一种方式
# UA伪装和请求地址
ua = UserAgent()
url = 'https://www.shicimingju.com/book/sanguoyanyi.html'
headers = {
"User-Agent":ua.chrome
}
获取目标网页的内容
# 获取章节目录的网页内容 proxies这个是我做的代理,防止被封IP的
HTML = requests.get(url=url, headers=headers,proxies=proxies).text
# BeautifulSoup实例化
soup = BeautifulSoup(HTML, 'lxml')
# 获取章节名所在的a标签
SectionTitlesTag = soup.select('.book-mulu a')
然后我们打印出来看看
[
<a href="/book/sanguoyanyi/1.html">第一回·宴桃园豪杰三结义 斩黄巾英雄首立功</a>
......
<a href="/book/sanguoyanyi/120.html">第一百二十回·荐杜预老将献新谋 降孙皓三分归一统</a>
]
这里我们就获取了关于目录里面所有的a标签,他的具体内容的网址链接我们也得到了
接下来我们遍历这个列表,获取到每个的a标签里面的内容再去进行获取他的正文内容
for index,i in enumerate(SectionTitlesTag):
# 获取标题名 和 标题链接
SectionTitles = i.text
# 因为a标签获取的链接地址是/book/sanguoyanyi/120.html,所以们这里要做一个拼接
contentUrl = 'https://www.shicimingju.com' + i.get('href')
# 获取正文内容
content = requests.get(url=contentUrl,headers=headers,proxies=proxies).text
soup1 = BeautifulSoup(content, 'lxml')
chapter_content = soup1.select('.chapter_content p')
接下来就是进行储存的时候了,我们把之前获取到的标题和正文内容分别储存进我们需要的文件里面
for index,i in enumerate(SectionTitlesTag):
# 标题名 和 标题链接
SectionTitles = i.text
contentUrl = 'https://www.shicimingju.com' + i.get('href')
# 写入标题
with open('三国演义.txt', 'a', encoding='utf-8') as file_handle:
file_handle.write(SectionTitles)
file_handle.write('\n')
# 获取正文内容
content = requests.get(url=contentUrl,headers=headers,proxies=proxies).text
soup1 = BeautifulSoup(content, 'lxml')
chapter_content = soup1.select('.chapter_content p')
# 写入正文内容
for i in chapter_content:
with open('三国演义.txt', 'a', encoding='utf-8') as f:
f.write(i.text)
f.write('\n')
OK储存完了之后,大体上我们就完成的差不多了,剩下就是进行一些代码的优化和美观的整理了,这里就不在赘述,话不多说,上代码~
源码
# 目录地址 url = 'https://www.shicimingju.com/book/sanguoyanyi.html'
import requests
from bs4 import BeautifulSoup
from fake_useragent import UserAgent
# UA 伪装 和 请求地址
ua = UserAgent()
url = 'https://www.shicimingju.com/book/sanguoyanyi.html'
headers = {
"User-Agent":ua.chrome
}
# 获取到页面的内容
def getHtml():
# 获取章节目录的网页内容
HTML = requests.get(url=url, headers=headers,proxies=proxies).text
# BeautifulSoup实例化
soup = BeautifulSoup(HTML, 'lxml')
# 获取章节名所在的a标签
SectionTitlesTag = soup.select('.book-mulu a')
for index,i in enumerate(SectionTitlesTag):
# 标题名 和 标题链接
SectionTitles = i.text
contentUrl = 'https://www.shicimingju.com' + i.get('href')
# 写入标题
with open('三国演义.txt', 'a', encoding='utf-8') as file_handle:
file_handle.write(SectionTitles)
file_handle.write('\n')
# 获取正文内容
content = requests.get(url=contentUrl,headers=headers,proxies=proxies).text
soup1 = BeautifulSoup(content, 'lxml')
chapter_content = soup1.select('.chapter_content p')
# 写入正文内容
for i in chapter_content:
with open('三国演义.txt', 'a', encoding='utf-8') as f:
f.write(i.text)
f.write('\n')
print('已加载完第', index+1, '章')
def main():
getHtml()
if __name__ == '__main__':
main()
如果你们觉得有用的话,那就多和我聊聊天吧,下面的评论给我留留言我就很开心啦