Python实现的爬取小说爬虫功能示例
作者:阳光Cherry梦 时间:2022-07-14 20:36:16
本文实例讲述了Python实现的爬取小说爬虫功能。分享给大家供大家参考,具体如下:
想把顶点小说网上的一篇持续更新的小说下下来,就写了一个简单的爬虫,可以爬取爬取各个章节的内容,保存到txt文档中,支持持续更新保存。需要配置一些信息,设置文档保存路径,书名等。写着玩,可能不大规范。
# coding=utf-8
import requests
from lxml import etree
from urllib.parse import urljoin
import re
import os
# 获取页面,并返回解析整理好的文本
def get_page(url):
response = requests.get(url, headers=header)
set_encoding(response)
text = parse_page(response.text)
return text
# 解析页面,将当前页面中的文字筛选出来
def parse_page(html):
title = re.findall('<div class="bookname">\s+<h1>(.+?)</h1>', html)[0]
content = re.findall('div id="content">(.*?)</div>', html, re.S)[0]
content = content.replace('<br />', '').replace(' ', ' ').replace('\r\n\r\n', '\r\n')
content = title + '\r\n' + content + '\r\n\r\n'
return content
# 将文本追加到file_path对应的txt中
def save_page(path, text):
with open(path, 'a', encoding='utf-8') as f:
f.write(text)
# 设置对response得到文本的解析编码为'gbk'
def set_encoding(response):
response.encoding = 'gbk'
# 从配置文件中获取当前保存的链接总数
def get_current_chapters_count(path):
# (1)第一次读配置文件可能没有创建,所以要支持没有文件创建文件的功能(2)如果文件存在,则不能清空,参考https://www.jb51.net/article/158740.htm
with open(path, 'a+') as f:
f.seek(0)
res = f.read()
if res == '':
return 0
else:
return int(res)
# 将保存的链接总数保存到配置文件中
def set_current_chapters_count(path, count):
with open(path, 'w') as f:
f.write(str(count))
# 需要配置的字典
config_dic = dict(start_url='http://www.booktxt.net/2_2220/', # 待下载小说的章节首页
latest_item=9, # 列出的所有章节链接中,前面几个链接为最新章节,一般为9个,爬取时避免与最后部分重复,所以前面9个链接不爬取
bookname='赘婿', # 待下载的小说名
folder_path='D:\\') #保存位置
domain = 'http://www.booktxt.net' # 顶点网域名
if __name__ == '__main__':
chapter_url_list = []
response = requests.get(config_dic['start_url'], headers=header)
set_encoding(response)
html = etree.HTML(response.text)
chapters = html.xpath('//dd')
print('所有链接' + str(len(chapters)))
chapters = chapters[config_dic['latest_item']:] # 前9章为最新章节,后面还会重复,这里去掉
print('不重复有效章节链接' + str(len(chapters)))
folder_path = config_dic['folder_path'] + config_dic['bookname']
if not os.path.exists(folder_path):
os.mkdir(folder_path)
file_path = folder_path + '\\' + config_dic['bookname'] + '.txt'
config_file_path = folder_path + '\\' + 'config.txt'
print('小说存储路径为:' + file_path)
print('配置文件存储路径为:' + config_file_path)
saved_count = get_current_chapters_count(config_file_path) # 获取目前保存的小说中已经包含的章节数
print('当前' + file_path + '中已经保存的章节总数' + str(saved_count))
if saved_count < len(chapters): # 说明有更新
set_current_chapters_count(config_file_path, len(chapters))
print('共更新 ' + str(len(chapters) - saved_count) + ' 章')
for c in chapters[saved_count:]: # 从上次保存的位置开始继续保存
url = c.xpath('a/@href')[0]
url = urljoin(domain, url)
txt = c.xpath('a/text()')[0]
chapter_url_list.append(url)
print(url)
print(txt)
save_page(file_path, get_page(url))
else:
print('小说还没有更新哦')
希望本文所述对大家Python程序设计有所帮助。
来源:https://blog.csdn.net/SunCherryDream/article/details/79070687
标签:Python,爬取小说,爬虫
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python三引号如何输入
2021-08-12 12:42:34
利用python爬取散文网的文章实例教程
2023-06-17 10:40:18
![](https://img.aspxhome.com/file/2023/0/120320_0s.jpg)
Golang 锁原理的简单实现
2024-04-28 09:18:03
![](https://img.aspxhome.com/file/2023/2/134002_0s.png)
Django分组聚合查询实例分享
2023-08-07 21:44:16
深入了解Python中Lambda函数的用法
2023-02-03 10:09:01
盘点Python 爬虫中的常见加密算法
2023-07-19 05:05:22
![](https://img.aspxhome.com/file/2023/5/124245_0s.jpg)
Python实现基于PIL和tesseract的验证码识别功能示例
2022-10-22 23:09:54
![](https://img.aspxhome.com/file/2023/4/94174_0s.png)
PHP实现批量生成App各种尺寸Logo
2023-07-23 03:59:59
如何用python 操作MongoDB数据库
2024-01-27 16:53:10
Python3 解释器的实现
2023-08-09 17:08:53
Python简单生成随机姓名的方法示例
2023-04-26 19:35:58
![](https://img.aspxhome.com/file/2023/0/112180_0s.png)
Python如何使用pymongo连接MongoDB数据库并进行相关操作
2023-05-12 01:21:49
![](https://img.aspxhome.com/file/2023/7/72017_0s.png)
python 对txt中每行内容进行批量替换的方法
2022-12-29 21:37:45
![](https://img.aspxhome.com/file/2023/1/67841_0s.jpg)
精通Javascript系列之数据类型 字符串
2024-04-17 09:49:59
Python根据成绩分析系统浅析
2023-08-02 20:25:07
![](https://img.aspxhome.com/file/2023/4/62024_0s.png)
python的sorted函数及使用解析
2022-03-02 05:52:21
使用C#配合ArcGIS Engine进行地理信息系统开发
2023-06-25 16:35:01
![](https://img.aspxhome.com/file/2023/6/61966_0s.jpg)
Python之父谈Python的未来形式
2022-02-05 11:10:42
SQL中DATEADD和DATEDIFF的用法示例介绍
2024-01-27 16:57:58
python matplotlib imshow热图坐标替换/映射实例
2023-05-04 03:46:54
![](https://img.aspxhome.com/file/2023/6/92086_0s.jpg)