脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - Python爬虫入门教程02之笔趣阁小说爬取

Python爬虫入门教程02之笔趣阁小说爬取

2021-08-28 00:36有趣的Python Python

这篇文章主要介绍了Python爬虫入门教程02之笔趣阁小说爬取,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

前言

本文的文字及图片来源于网络,仅供学习、交流使用,不具有任何商业用途,如有问题请及时联系我们以作处理。

前文

01、python爬虫入门教程01:豆瓣top电影爬取

基本开发环境

  • python 3.6
  • pycharm

相关模块的使用

  • request
  • sparsel

安装python并添加到环境变量,pip安装需要的相关模块即可。

Python爬虫入门教程02之笔趣阁小说爬取

单章爬取

Python爬虫入门教程02之笔趣阁小说爬取

一、明确需求

爬取小说内容保存到本地

  • 小说名字
  • 小说章节名字
  • 小说内容
?
1
2
# 第一章小说url地址
url = 'http://www.biquges.com/52_52642/25585323.html'
?
1
2
3
4
5
6
url = 'http://www.biquges.com/52_52642/25585323.html'
headers = {
 'user-agent': 'mozilla/5.0 (windows nt 10.0; wow64) applewebkit/537.36 (khtml, like gecko) chrome/81.0.4044.138 safari/537.36'
}
response = requests.get(url=url, headers=headers)
print(response.text)

Python爬虫入门教程02之笔趣阁小说爬取

请求网页返回的数据中出现了乱码,这就需要我们转码了。

加一行代码自动转码。

?
1
response.encoding = response.apparent_encoding

Python爬虫入门教程02之笔趣阁小说爬取

三、解析数据

Python爬虫入门教程02之笔趣阁小说爬取

根据css选择器可以直接提取小说标题以及小说内容。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
def get_one_novel(html_url):
 # 调用请求网页数据函数
 response = get_response(html_url)
 # 转行成selector解析对象
 selector = parsel.selector(response.text)
 # 获取小说标题
 title = selector.css('.bookname h1::text').get()
 # 获取小说内容 返回的是list
 content_list = selector.css('#content::text').getall()
 # ''.join(列表) 把列表转换成字符串
 content_str = ''.join(content_list)
 print(title, content_str)
 
if __name__ == '__main__':
 url = 'http://www.biquges.com/52_52642/25585323.html'
 get_one_novel(url)

Python爬虫入门教程02之笔趣阁小说爬取

四、保存数据(数据持久化)

使用常用的保存方式: with open

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
def save(title, content):
 """
 保存小说
 :param title: 小说章节标题
 :param content: 小说内容
 :return:
 """
 # 路径
 filename = f'{title}\\'
 # os 内置模块,自动创建文件夹
 if os.makedirs(filename):
 os.mkdir()
 # 一定要记得加后缀 .txt mode 保存方式 a 是追加保存 encoding 保存编码
 with open(filename + title + '.txt', mode='a', encoding='utf-8') as f:
 # 写入标题
 f.write(title)
 # 换行
 f.write('\n')
 # 写入小说内容
 f.write(content)

Python爬虫入门教程02之笔趣阁小说爬取
Python爬虫入门教程02之笔趣阁小说爬取

保存一章小说,就这样写完了,如果想要保存整本小说呢?

整本小说爬虫

既然爬取单章小说知道怎么爬取了,那么只需要获取小说所有单章小说的url地址,就可以爬取全部小说内容了。

Python爬虫入门教程02之笔趣阁小说爬取

所有的单章的url地址都在 dd 标签当中,但是这个url地址是不完整的,所以爬取下来的时候,要拼接url地址。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
def get_all_url(html_url):
 # 调用请求网页数据函数
 response = get_response(html_url)
 # 转行成selector解析对象
 selector = parsel.selector(response.text)
 # 所有的url地址都在 a 标签里面的 href 属性中
 dds = selector.css('#list dd a::attr(href)').getall()
 for dd in dds:
 novel_url = 'http://www.biquges.com' + dd
 print(novel_url)
 
 
if __name__ == '__main__':
 url = 'http://www.biquges.com/52_52642/index.html'
 get_all_url(url)

Python爬虫入门教程02之笔趣阁小说爬取

这样就获取了所有的小说章节url地址了。

爬取全本完整代码

?
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import requests
import parsel
from tqdm import tqdm
 
 
def get_response(html_url):
 headers = {
 'user-agent': 'mozilla/5.0 (windows nt 10.0; wow64) applewebkit/537.36 (khtml, like gecko) chrome/81.0.4044.138 safari/537.36'
 }
 response = requests.get(url=html_url, headers=headers)
 response.encoding = response.apparent_encoding
 return response
 
 
def save(novel_name, title, content):
 """
 保存小说
 :param title: 小说章节标题
 :param content: 小说内容
 :return:
 """
 filename = f'{novel_name}' + '.txt'
 # 一定要记得加后缀 .txt mode 保存方式 a 是追加保存 encoding 保存编码
 with open(filename, mode='a', encoding='utf-8') as f:
 # 写入标题
 f.write(title)
 # 换行
 f.write('\n')
 # 写入小说内容
 f.write(content)
 
 
def get_one_novel(name, novel_url):
 # 调用请求网页数据函数
 response = get_response(novel_url)
 # 转行成selector解析对象
 selector = parsel.selector(response.text)
 # 获取小说标题
 title = selector.css('.bookname h1::text').get()
 # 获取小说内容 返回的是list
 content_list = selector.css('#content::text').getall()
 # ''.join(列表) 把列表转换成字符串
 content_str = ''.join(content_list)
 save(name, title, content_str)
 
 
def get_all_url(html_url):
 # 调用请求网页数据函数
 response = get_response(html_url)
 # 转行成selector解析对象
 selector = parsel.selector(response.text)
 # 所有的url地址都在 a 标签里面的 href 属性中
 dds = selector.css('#list dd a::attr(href)').getall()
 # 小说名字
 novel_name = selector.css('#info h1::text').get()
 for dd in tqdm(dds):
 novel_url = 'http://www.biquges.com' + dd
 get_one_novel(novel_name, novel_url)
 
if __name__ == '__main__':
 novel_id = input('输入书名id:')
 url = f'http://www.biquges.com/{novel_id}/index.html'
 get_all_url(url)

Python爬虫入门教程02之笔趣阁小说爬取
Python爬虫入门教程02之笔趣阁小说爬取

到此这篇关于python爬虫入门教程02之笔趣阁小说爬取的文章就介绍到这了,更多相关python爬虫笔趣阁小说爬取内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://www.cnblogs.com/Qqun821460695/archive/2021/01/23/14318657.html

延伸 · 阅读

精彩推荐