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 pyqt 教程(Python+PyQt5实现美剧爬虫可视工具的方法)
- python爬取数据总结(python3爬虫学习之数据存储txt的案例详解)
- python爬虫爬取知乎(详解用python写网络爬虫-爬取新浪微博评论)
- python爬取在线评论(Python爬虫实现爬取百度百科词条功能实例)
- python爬虫经典步骤(详解python爬虫系列之初识爬虫)
- pythonrequests爬虫使用教程(Python 通过requests实现腾讯新闻抓取爬虫的方法)
- python爬虫并保存excel实例(Python实现爬取亚马逊数据并打印出Excel文件操作示例)
- 微信公众号文章 爬虫(python抓取搜狗微信公众号文章)
- python实现网络爬虫的步骤(Python实现的爬取小说爬虫功能示例)
- python爬虫怎么设置代理ip(python爬虫简单的添加代理进行访问的实现代码)
- python3.8爬虫需要的包(python爬取基于m3u8协议的ts文件并合并)
- python爬虫入门代码(python爬虫基础教程:requests库二代码实例)
- python3爬虫代码(Python3爬楼梯算法示例)
- python爬虫10分钟教程(浅谈Python爬虫基本套路)
- nginx过滤器漏洞(Nginx反爬虫策略,防止UA抓取网站)
- python 爬虫招聘(Python3获取拉勾网招聘信息的方法实例)
- 泪目 这位 刷屏 的英雄,是全椒人的骄傲(泪目这位刷屏)
- 人从众 火炎焱 全椒再现 正月十六走太平 的魅力(人从众火炎焱全椒再现)
- 官宣 全椒籍明星许海峰 奚秀兰 方芳 王璐瑶携手回家 走太平(全椒籍明星许海峰)
- 以前全椒人是怎么过冬的 满满都是回忆(以前全椒人是怎么过冬的)
- NVIDIA显卡份额冲上88 A饭发愁 游戏优化恐没A卡份了(NVIDIA显卡份额冲上88A饭发愁)
- AMD YES A卡还是N卡 A卡和N卡的区别(AMDYESA卡还是N卡)
热门推荐
- FileZilla Server搭建FTP服务器配置及425错误与TLS警告解决方法详解(FileZilla Server搭建FTP服务器配置及425错误与TLS警告解决方法详解)
- Visual Studio使用Git进行源代码管理
- mysql拆分成多行(mysql单字段多值分割和合并的处理方法)
- 在mysql语法中用来修改数据的命令(explain命令为什么可能会修改MySQL数据)
- python中的迭代器详解(Python通过for循环理解迭代器和生成器实例详解)
- 如何看idea连接mysql数据库(IDEA 链接Mysql数据库并执行查询操作的完整代码)
- dedecms上传附件大小限制(dedecms删除文章同时也删除附件的修改方法)
- hive数值转字符串(hive中将string数据转为bigint的操作)
- drools动态执行规则(使用swoole 定时器变更超时未支付订单状态的解决方案)
- html5最好用语义元素(HTML5语义化元素你真的用对了吗)
排行榜
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9