python实现网络爬虫的步骤(Python实现的爬取小说爬虫功能示例)
类别:脚本大全 浏览量:667
时间:2021-10-24 10:39:32 python实现网络爬虫的步骤
Python实现的爬取小说爬虫功能示例本文实例讲述了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( '<li class="bookname">\s+<h1>(.+?)</h1>' , html)[ 0 ] content = re.findall( 'li id="content">(.*?)</li>' , 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)如果文件存在,则不能清空,参考http://www.zzvips.com/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爬虫10分钟教程(浅谈Python爬虫基本套路)
- python pyqt 教程(Python+PyQt5实现美剧爬虫可视工具的方法)
- python3.8爬虫需要的包(python爬取基于m3u8协议的ts文件并合并)
- python爬虫音乐代码(详解python selenium 爬取网易云音乐歌单名)
- Python HTML解析器BeautifulSoup用法实例详解【爬虫解析器】(Python HTML解析器BeautifulSoup用法实例详解爬虫解析器)
- scrapy爬虫原理(浅谈Scrapy网络爬虫框架的工作原理和数据采集)
- python scrapy 框架原理(一步步教你用python的scrapy编写一个爬虫)
- python爬取豆瓣评分排行榜(Python爬虫——爬取豆瓣电影Top250代码实例)
- python获取天气源(Python3爬虫之自动查询天气并实现语音播报)
- python 爬虫招聘(Python3获取拉勾网招聘信息的方法实例)
- python 操作html(Python HTML解析模块HTMLParser用法分析爬虫工具)
- 阿里云虚拟主机带公网地址吗(阿里云虚拟主机被搜索引擎爬虫访问耗费大量流量解决方法)
- pycharm 爬虫的数据存在哪了(利用PyCharm Profile分析异步爬虫效率详解)
- 如何查看python beautifulsoup(Python爬虫beautifulsoup4常用的解析方法总结)
- python爬虫书籍经典(Python利用lxml模块爬取豆瓣读书排行榜的方法与分析)
- python3爬虫实例代码(python3通过selenium爬虫获取到dj商品的实例代码)
- 小米推出米兔儿童电话手表奥特曼版,799 元,支持微信 QQ(小米推出米兔儿童电话手表奥特曼版)
- 贾怀胤唱《白龙马》 炸场 了 没想到京剧还能这么玩(贾怀胤唱白龙马)
- 白龙马的改编学生版,快来看看(白龙马的改编学生版)
- 萌娃唱《白龙马》走红,那生动的小表情,网友直呼 简直是戏精(萌娃唱白龙马走红)
- 朱鹤松被不断认可,凤凰传奇玲花喊话岳云鹏,索要老朱演出门票(朱鹤松被不断认可)
- 元宵晚会槽点多,芒果台上来就假唱,岳云鹏不说相声改评书了(元宵晚会槽点多)
热门推荐
- csstable列宽固定(css display table 自适应高度、宽度问题的解决)
- 阿里云服务器总被攻击怎么办(香港云服务器遭遇恶意攻击怎么处理?)
- apache服务器的主配置(Apache中.htaccess文件功能)
- sqlserver 怎么排序(SQLserver排序规则基本概念探索)
- php生命周期表(PHP中md5函数的用法讲解)
- web接口测试中需要测试的几个点
- linux安装phpstudy(PHPStudy下如何为Apache安装SSL证书的方法步骤)
- css格式设置(CSS中em的正确打开方式详解)
- python发送钉钉消息(钉钉群自定义机器人消息Python封装的实例)
- sql查询重复记录
排行榜
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9