代码

# 需要的库
import requests
from lxml import etree
from multiprocessing import Pool
import os
# 请求头
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 = './八零电子书/'
if not os.path.exists(pathname):
os.mkdir(pathname)
# 获取书籍列表
def get_booklist(url):
try:
response = requests.get(url=url,headers=headers)
etrees = etree.HTML(response.text)
sum = etrees.xpath('//a[@class="last"]/text()')[0]
booklist = etrees.xpath('//div[@class="book_bg"]/a/@href')
pool.map(get_book,booklist)
urls = ['http://www.quanshuwang.com/list/3_{}.html'.format(i) for i in range(2,int(sum)+1)]
pool.map(get_booklist,urls)
except Exception:
print('get_booklist failed')
# 获取具体书籍
def get_book(url):
try:
response = requests.get(url=url, headers=headers)
etrees = etree.HTML(response.text)
mulu = etrees.xpath('//a[@id="read_book"]/@href')[1]
get_mulu(mulu)
except Exception:
print('get_book failed')
# 获取书籍目录
def get_mulu(url):
try:
response = requests.get(url=url, headers=headers)
etrees = etree.HTML(response.text)
zhangjie = etrees.xpath('//div[@id="yulan"]/li/a/@href')
for i in zhangjie:
get_content(i)
except Exception:
print('get_mulu failed')
# 获取书籍内容
def get_content(url):
try:
response = requests.get(url=url, headers=headers)
etrees = etree.HTML(response.text.encode(response.encoding).decode(response.apparent_encoding))
book_name = etrees.xpath('//p[@class="text"]/a/text()')[1]
zhangjie = etrees.xpath('//div[@class="date"]/h1/text()')[0]
contents = etrees.xpath('//div[@id="content"]/text()')
print(zhangjie+'..正在下载')
f = open(pathname+book_name+'.txt','a+',encoding='utf-8')
f.write(zhangjie+'\n\n')
for con in contents:
f.write(con+'\n')
f.close()
except Exception:
print('get_content failed') # 程序入口
if __name__ == '__main__':
url = 'https://www.80txt.la/sort5/1.html'
# 创建进程池
pool = Pool()
# 启动函数
get_booklist(url)

控制台输出

E:\anaconda\python.exe E:/练习/最后阶段/0809/八零电子书.py
1第一章 捡到个小雌性..正在下载
01 遗嘱..正在下载
第一章 捡了东西不一定能换到钱..正在下载
2第二章 摔出了地球..正在下载
02 异变..正在下载
3第三章 这是个高科技世界..正在下载
第二章 爷爷!您是我的亲爷爷..正在下载
03 手镯..正在下载
第三章 不在新手村混的新手..正在下载
4第四章 所谓杌力..正在下载
第一章 我会打架..正在下载
04长生..正在下载

打开文件夹查看是否下载成功

done。

xpath+多进程爬取八零电子书百合之恋分类下所有小说。的更多相关文章

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

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

  2. 使用Xpath+多进程爬取诗词名句网的史书典籍类所有文章。update~

    上次写了爬取这个网站的程序,有一些地方不完善,而且爬取速度较慢,今天完善一下并开启多进程爬取,速度就像坐火箭.. # 需要的库 from lxml import etree import reques ...

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

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

  4. 代理ip的使用以及多进程爬取

    一.代理皮的简单使用 简单的看一二例子即可 import requests #代理ip 高频的ip容易被封,所以使用ip代理 #免费代理 ip:www.goubanjia.com 快代理 西祠代理 h ...

  5. python+BeautifulSoup+多进程爬取糗事百科图片

    用到的库: import requests import os from bs4 import BeautifulSoup import time from multiprocessing impor ...

  6. requests+xpath+map爬取百度贴吧

    # requests+xpath+map爬取百度贴吧 # 目标内容:跟帖用户名,跟帖内容,跟帖时间 # 分解: # requests获取网页 # xpath提取内容 # map实现多线程爬虫 impo ...

  7. python爬取 “得到” App 电子书信息

    前言 文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者: 静觅 崔庆才 PS:如有需要Python学习资料的小伙伴可以加点击下 ...

  8. python+正则+多进程爬取糗事百科图片

    话不多说,直接上代码: # 需要的库 import requests import re import os from multiprocessing import Pool # 请求头 header ...

  9. 使用进程池模拟多进程爬取url获取数据,使用进程绑定的回调函数去处理数据

    1 # 使用requests请求网页,爬取网页的内容 2 3 # 模拟使用进程池模拟多进程爬取网页获取数据,使用进程绑定的回调函数去处理数据 4 5 import requests 6 from mu ...

随机推荐

  1. scala 抽象类

    package com.jason.qianfeng abstract class Person(val gender: String) { val name: String val age: Int ...

  2. (CSDN迁移) jFinal无法启动_JFinalDemoGenerator问题解决办法

    Eclipse -> 项目右键 -> Build Path -> Config Build Path -> Source -> Add Folder, 将a_little ...

  3. javascript jssdk微信上传一张图片的方法

    javascript jssdk微信上传一张图片的方法 <pre> wx.chooseImage({ count: 1, // 默认9 sizeType: ['original', 'co ...

  4. Harbor 开机自启遇到的问题

    Harbor 是一个企业级的 Docker 私有仓库项目,它本身由多个 Docker Containers 组成,通过 docker-compose 管理 containers 之间的依赖关系. 安装 ...

  5. python_进程池以及线程池

    可以重复利用的线程 直接上代码 from threading import Thread, current_thread from queue import Queue # 重写线程类 class M ...

  6. Git diff (---和+++具体解释)(转)

    转自:https://blog.csdn.net/lovezbs/article/details/46492933

  7. Scala Spark WordCount

    Scala所需依赖 <dependency> <groupId>org.scala-lang</groupId> <artifactId>scala-l ...

  8. 给自己看的Cache,三段代码

    此篇是我记录代码的一个草稿,不是一篇正式的博文,误点的别介意啊. 公司的框架中Cache实现文件: (1)CacheUtil.cs using System.Collections.Generic; ...

  9. The XOR Largest Pair(tire树)

    题目 The XOR Largest Pair 解析 一年前听学长讲这道题,什么01trie,好高级啊,所以没学,现在一看.... 看到xor就应该想到二进制,一看数据\(A_i< 2^{31} ...

  10. Django使用 django-allauth实现第三方登陆

    Django使用 django-allauth实现第三方登陆 这里我们使用 django-allauth 模块来实现第三方账号验证登录,官方文档如下:https://django-allauth.re ...