使用Xpath+多进程爬取诗词名句网的史书典籍类所有文章。update~
上次写了爬取这个网站的程序,有一些地方不完善,而且爬取速度较慢,今天完善一下并开启多进程爬取,速度就像坐火箭。。
# 需要的库
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)
控制台输出;

查看文件夹,可以发现文件是多个多个的同时在下载;

done。
使用Xpath+多进程爬取诗词名句网的史书典籍类所有文章。update~的更多相关文章
- 使用Xpath爬虫库下载诗词名句网的史书典籍类所有文章。
# 需要的库 from lxml import etree import requests # 请求头 headers = { 'User-Agent': 'Mozilla/5.0 (Windows ...
- xpath+多进程爬取网易云音乐热歌榜。
用到的工具,外链转换工具 网易云网站直接打开源代码里面并没有对应的歌曲信息,需要对url做处理, 查看网站源代码路径:发现把里面的#号去掉会显示所有内容, 右键打开的源代码路径:view-source ...
- xpath+多进程爬取全书网纯爱耽美类别的所有小说。
# 需要的库 import requests from lxml import etree from multiprocessing import Pool import os # 请求头 heade ...
- xpath+多进程爬取八零电子书百合之恋分类下所有小说。
代码 # 需要的库 import requests from lxml import etree from multiprocessing import Pool import os # 请求头 he ...
- 爬取斗图网图片,使用xpath格式来匹配内容,对请求伪装成浏览器, Referer 防跨域请求
6.21自我总结 一.爬取斗图网 1.摘要 使用xpath匹配规则查找对应信息文件 将请求伪装成浏览器 Referer 防跨域请求 2.爬取代码 #导入模块 import requests #爬取网址 ...
- python爬虫基础应用----爬取校花网视频
一.爬虫简单介绍 爬虫是什么? 爬虫是首先使用模拟浏览器访问网站获取数据,然后通过解析过滤获得有价值的信息,最后保存到到自己库中的程序. 爬虫程序包括哪些模块? python中的爬虫程序主要包括,re ...
- selenium爬取煎蛋网
selenium爬取煎蛋网 直接上代码 from selenium import webdriver from selenium.webdriver.support.ui import WebDriv ...
- Scrapy实战篇(一)之爬取链家网成交房源数据(上)
今天,我们就以链家网南京地区为例,来学习爬取链家网的成交房源数据. 这里推荐使用火狐浏览器,并且安装firebug和firepath两款插件,你会发现,这两款插件会给我们后续的数据提取带来很大的方便. ...
- Python Scrapy 爬取煎蛋网妹子图实例(一)
前面介绍了爬虫框架的一个实例,那个比较简单,这里在介绍一个实例 爬取 煎蛋网 妹子图,遗憾的是 上周煎蛋网还有妹子图了,但是这周妹子图变成了 随手拍, 不过没关系,我们爬图的目的是为了加强实战应用,管 ...
随机推荐
- spring中RequestBody注解接收参数时用JSONField转参数名无效问题
问题: 在springboot项目中使用@RequestBody注解接收post请求中body里的json参数的情况.即: @RequestMapping(value = "/get-use ...
- [ kvm ] 三种基础网络模型创建及分析
1. 前言 最近在模拟生产环境在做测试,本来准备用 vmware 直接来实现的,本着学以致用的道理,选择直接在linux 环境使用 kvm 来模拟测试,遇到的第一个问题就是,网络环境的模拟.这里对比v ...
- c# .net framework 4.5.2 , Quartz.NET 3.0.7
参考了:https://www.cnblogs.com/personblog/p/11277527.html, https://www.jianshu.com/p/b8e7e4deb60a .NET ...
- centos6 rpm安装mysql 5.7
安装过程中需要进行系统补包操作 [root@cent6zbx ~]# rpm -qa |grep mysql mysql-libs--.el6_6.x86_64 [root@cent6zbx ~]# ...
- nginx配置优化提高并发量
1 nginx配置优化提高并发量 worker_processes 2; 这个按照CPU的核数来决定 2 worker_connections 65535; 这个一般设置65535即可 每个进程允许的 ...
- 【转帖】全网最通俗易懂的Kafka入门
全网最通俗易懂的Kafka入门 http://www.itpub.net/2019/12/04/4597/ 前言 只有光头才能变强. 文本已收录至我的GitHub仓库,欢迎Star:https://g ...
- Linux Docker Introduction
Setup on Ubuntu. 前提条件: Docker需要两个重要的安装要求: 它仅适用于64位Linux安装,注意:是64位的Linux系统. 它需要Linux内核版本3.10或更高版本. 要查 ...
- string字符串成员函数
string字符串成员函数 string str1="aaa"; char c='c'; str1.assign("ABCAAAAAAABBBBB");//替换 ...
- 远程登录Linux系统(使用xshell),远程上传加载文件(使用Xftp)
一.Xshell(远程登录Linux系统) 1.安装xshell 自己百度找安装包 2.连接登录 1.连接前提 需要Linux开启一个sshd的服务,监听22号端口,一般默认是开启的 查看是否开启: ...
- memcached源码分析三-libevent与命令解析
转载请注明出处https://www.cnblogs.com/yang-zd/p/11352833.html,谢谢合作! 前面已经分析了memcached中的slabs内存管理及缓存对象如何利用ite ...