我是靠谱客的博主 热心信封,这篇文章主要介绍python网络爬虫(第五章:实战5:bs4之数据解析)实战1. 爬取三国演义小说所有章节标题和章节内容,现在分享给大家,希望可以做个参考。

实战1. 爬取三国演义小说所有章节标题和章节内容

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# https://www.shicimingju.com/book/sanguoyanyi.html import requests from bs4 import BeautifulSoup if __name__ == "__main__": #1.指定url url ='https://www.shicimingju.com/book/sanguoyanyi.html' header = { 'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36' } #2.发送get请求 response = requests.get(url=url,headers=header).text.encode('ISO-8859-1') #requests.get() 获取的是响应对象 #.text获取的是响应数据 # 3.实例化BeautifulSoup对象,需要将页面源码数据加载到该对象中 soup = BeautifulSoup(response,'lxml') # 4.解析章节标题 li_list = soup.select('.book-mulu > ul > li > a') fp = open('./sanguoyan.txt', 'w', encoding='utf-8') #创建txt格式的文件夹 for li in li_list: #解析章节标题 title = li.string #li标签下的a标签中的直系内容,则使用string #准备解析章节内容: detail_url = 'https://www.shicimingju.com' + li['href'] #对详情页发送请求,解析出章节内容 detail_page_text = requests.get(url=detail_url,headers=header).text.encode('ISO-8859-1') #解析出详情页中相关的章节内容 detail_soup = BeautifulSoup(detail_page_text,'lxml') #解析到了章节的内容 detail_tag = detail_soup.find('div',class_='chapter_content').text fp.write(title+":"+ detail_tag +'n') print(title,'成功抓取到!!!')

最后

以上就是热心信封最近收集整理的关于python网络爬虫(第五章:实战5:bs4之数据解析)实战1. 爬取三国演义小说所有章节标题和章节内容的全部内容,更多相关python网络爬虫(第五章:实战5:bs4之数据解析)实战1.内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(93)

评论列表共有 0 条评论

立即
投稿
返回
顶部