上次写了爬取这个网站的程序,有一些地方不完善,而且爬取速度较慢,今天完善一下并开启多进程爬取,速度就像坐火箭。。

# 需要的库
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~的更多相关文章

  1. 使用Xpath爬虫库下载诗词名句网的史书典籍类所有文章。

    # 需要的库 from lxml import etree import requests # 请求头 headers = { 'User-Agent': 'Mozilla/5.0 (Windows ...

  2. xpath+多进程爬取网易云音乐热歌榜。

    用到的工具,外链转换工具 网易云网站直接打开源代码里面并没有对应的歌曲信息,需要对url做处理, 查看网站源代码路径:发现把里面的#号去掉会显示所有内容, 右键打开的源代码路径:view-source ...

  3. xpath+多进程爬取全书网纯爱耽美类别的所有小说。

    # 需要的库 import requests from lxml import etree from multiprocessing import Pool import os # 请求头 heade ...

  4. xpath+多进程爬取八零电子书百合之恋分类下所有小说。

    代码 # 需要的库 import requests from lxml import etree from multiprocessing import Pool import os # 请求头 he ...

  5. 爬取斗图网图片,使用xpath格式来匹配内容,对请求伪装成浏览器, Referer 防跨域请求

    6.21自我总结 一.爬取斗图网 1.摘要 使用xpath匹配规则查找对应信息文件 将请求伪装成浏览器 Referer 防跨域请求 2.爬取代码 #导入模块 import requests #爬取网址 ...

  6. python爬虫基础应用----爬取校花网视频

    一.爬虫简单介绍 爬虫是什么? 爬虫是首先使用模拟浏览器访问网站获取数据,然后通过解析过滤获得有价值的信息,最后保存到到自己库中的程序. 爬虫程序包括哪些模块? python中的爬虫程序主要包括,re ...

  7. selenium爬取煎蛋网

    selenium爬取煎蛋网 直接上代码 from selenium import webdriver from selenium.webdriver.support.ui import WebDriv ...

  8. Scrapy实战篇(一)之爬取链家网成交房源数据(上)

    今天,我们就以链家网南京地区为例,来学习爬取链家网的成交房源数据. 这里推荐使用火狐浏览器,并且安装firebug和firepath两款插件,你会发现,这两款插件会给我们后续的数据提取带来很大的方便. ...

  9. Python Scrapy 爬取煎蛋网妹子图实例(一)

    前面介绍了爬虫框架的一个实例,那个比较简单,这里在介绍一个实例 爬取 煎蛋网 妹子图,遗憾的是 上周煎蛋网还有妹子图了,但是这周妹子图变成了 随手拍, 不过没关系,我们爬图的目的是为了加强实战应用,管 ...

随机推荐

  1. 《深入理解Linux内核》 读书笔记

    深入理解Linux内核 读书笔记 一.概论 操作系统基本概念 多用户系统 允许多个用户登录系统,不同用户之间的有私有的空间 用户和组 每个用于属于一个组,组的权限和其他人的权限,和拥有者的权限不一样. ...

  2. 如何在jupyter中使用Python2和Python3

    首先通过 pip2 install ipython notebook pip3 install ipython notebook 分别安装ipython notebook,安装命令还是推荐使用国内的豆 ...

  3. Java操作fastDFS

    一.加入Maven依赖 <dependency> <groupId>org.csource</groupId> <artifactId>fastdfs- ...

  4. Python实现堆

    堆 (heap) 是一种经过排序的完全二叉树,其中任一非叶子节点的值均不大于(或不小于)其左孩子和右孩子节点的值. 注:定义来自百度百科. 堆,又被为优先队列(priority queue).尽管名为 ...

  5. CORS解决跨域问题(403问题)

    1.什么是跨域问题? 跨域问题是浏览器对于ajax请求的一种安全限制:一个页面发起的ajax请求,只能是用当前页同域名同端口的路径,这能有效的阻止跨站攻击. 2.跨域问题出现的条件: 1.跨域问题是a ...

  6. FORMAT 的用法

    https://www.cnblogs.com/gaodu2003/archive/2008/12/22/1359927.html Format 格式指令具有以下的形式:"%" [ ...

  7. C++ Primer第五版(中文带书签)

    本想发github的(链接更稳定),但是大小超出限制了. 本文件为扫描件,还是在我找了大半天之后的结果.能找到的免费的貌似都是扫描件,在看了一百多页之后(我不喜欢文字不能选中的感觉),我果断买了纸质书 ...

  8. ALV报表——发送Excel报表邮件

    ABAP发送报表邮件 运行效果: 代码: *&---------------------------------------------------------------------* *& ...

  9. Typora使用技巧系列:(1)

    Typora使用技巧(1) 刚刚开了博客怎么说也要学一下markdown语法什么的吧,使用的是编译器是Typora,之后有空会陆续更新的 切换到源代码模:(ctrl + /)临时切换到源代码模式,再按 ...

  10. jquery加载数据时显示loading加载动画特效

    插件下载:http://www.htmleaf.com/jQuery/Layout-Interface/201505061788.html 插件使用: 使用该loading加载插件首先要引入jQuer ...