使用Xpath+多进程爬取诗词名句网的史书典籍类所有文章。update~
2019-08-09 13:24
106 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_42625143/article/details/98953892
上次写了爬取这个网站的程序,有一些地方不完善,而且爬取速度较慢,今天完善一下并开启多进程爬取,速度就像坐火箭。。
# 需要的库 from lxml import etree import requests from multiprocessing import Pool # 请求头 headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36' } # 保存文本的地址 pathname=r'E:\爬虫\诗词名句网\\' # 获取书籍名称的函数 def get_book(url): try: response = requests.get(url,headers) etrees = etree.HTML(response.text) url_infos = etrees.xpath('//div[@class="bookmark-list"]/ul/li') urls = [] for i in url_infos: url_info = i.xpath('./h2/a/@href') book_name = i.xpath('./h2/a/text()')[0] print('开始下载.'+book_name) urls.append('http://www.shicimingju.com' + url_info[0]) # print('http://www.shicimingju.com'+url_info[0]) # get_index('http://www.shicimingju.com'+url_info[0]) # 开启多进程 pool.map(get_index,urls) except Exception: print('get_book failed') # 获取书籍目录的函数 def get_index(url): try: response = requests.get(url, headers) etrees = etree.HTML(response.text) url_infos = etrees.xpath('//div[@class="book-mulu"]/ul/li') for i in url_infos: url_info = i.xpath('./a/@href') # print('http://www.shicimingju.com' + url_info[0]) get_content('http://www.shicimingju.com' + url_info[0]) except Exception as e: print(e) # 获取书籍内容并写入.txt文件 def get_content(url): try: response = requests.get(url, headers) etrees = etree.HTML(response.text) title = etrees.xpath('//div[@class="www-main-container www-shadow-card "]/h1/text()')[0] content = etrees.xpath('//div[@class="chapter_content"]/p/text()') if not content: content = etrees.xpath('//div[@class="chapter_content"]/text()') content = ''.join(content) book_name = etrees.xpath('//div[@class="nav-top"]/a[3]/text()')[0] with open(pathname + book_name + '.txt', 'a+', encoding='utf-8') as f: f.write(title + '\n\n' + content + '\n\n\n') print(title + '..下载完成') else: content = ''.join(content) book_name=etrees.xpath('//div[@class="nav-top"]/a[3]/text()')[0] with open(pathname+book_name+'.txt','a+',encoding='utf-8') as f: f.write(title+'\n\n'+content+'\n\n\n') print(title+'..下载完成') except Exception: print('get_content failed') # 程序入口 if __name__ == '__main__': url = 'http://www.shicimingju.com/book/' # 开启进程池 pool = Pool() # 启动函数 get_book(url)
控制台输出;
查看文件夹,可以发现文件是多个多个的同时在下载;
相关文章推荐
- 检索进程所使用的所有模块的列表
- 使用python抓取CSDN关注人的所有发布的文章
- Linux 统计当前所有进程使用swap分区的情况
- 查看文件目录下所有文件的使用进程
- 【翻译自mos文章】OGG replicat 进程使用的 TCP port
- Windows系统使用vbs脚本或bat脚本强制杀死指定所有进程 vbs实现循环持续写入内容到vbs打开开的记事本 使用vbs、bat添加windows计划任务 使用cmd schtasks命令添加windows计划任务
- 【翻译自mos文章】OGG replicat 进程使用的 TCP 端口
- Linux中使用ps、awk、sh一起批量杀死所有的dotnet进程。
- kafka的receive方式实现WordCount,使用updateStateByKey函数,累加所有批次的wordCount
- shell之使用eval模拟一维数组保存进程pids,并调用wait等待所有进程执行完返回
- 使用JAVA爬取博客里面的所有文章
- XPath语法 在C#中使用XPath示例 【转http://www.cnblogs.com/yukaizhao/archive/2011/07/25/xpath.html】非常详细的文章
- 使用TDD方式开发:根据博客ID列出CSDN所有博客文章
- calcHist的使用中严重的错误和误区!!!!!!!为什么所有的文章都没有指出??
- python量化分析系列之---python分别使用多线程和多进程获取所有股票实时数据
- python分别使用多线程和多进程获取所有股票实时数据
- 使用Python正则表达式从文章中取出所有图片路径
- python爬虫——使用xpath爬取搜狗微信文章
- Softice使用指南(异域の蜗牛注:经典的调试工具,几乎能调试所有程序.感谢ddcrack让我看了这么好的文章.分享)
- 所有进程的名字和他使用的模块名以及其它信息