zoukankan      html  css  js  c++  java
  • xpath+多进程爬取全书网纯爱耽美类别的所有小说。

    # 需要的库
    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('//ul[@class="seeWell cf"]/li')
            book_list = []
            for books in booklist:
                book = books.xpath('./a/@href')[0]
                book_list.append(book)
            pool.map(get_book,book_list)
            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.content.decode("gb18030"))
            book_name = etrees.xpath('//div[@class="b-info"]/h1/text()')[0]
            if os.path.exists(pathname+book_name+'.txt'):
                print(book_name+'.书籍已存在,如需重新下载请删除原文件')
                return None
            book = etrees.xpath('//div[@class="b-oper"]/a/@href')[0]
            get_mulu(book)
        except Exception:
            print('get_book failed')
    # 获取书籍目录
    def get_mulu(url):
        try:
            response = requests.get(url=url, headers=headers)
            etrees = etree.HTML(response.text)
            book = etrees.xpath('//div[@class="clearfix dirconone"]/li')
            for i in book:
                book = i.xpath('./a/@href')[0]
                get_content(book)
        except Exception:
            print('get_mulu failed')
    # 获取并写入书籍内容
    def get_content(url):
        try:
            response = requests.get(url=url, headers=headers)
            etrees = etree.HTML(response.content.decode("gb18030"))
            title = etrees.xpath('//a[@class="article_title"]/text()')[0]
            zhangjie = etrees.xpath('//strong[@class="l jieqi_title"]/text()')[0]
            contents = etrees.xpath('//div[@class="mainContenr"]/text()')
            content = ''.join(contents)
            with open(pathname+title+'.txt','a+',encoding='utf-8') as f:
                f.write(zhangjie+'
    
    '+content+'
    
    ')
            print('正在下载:',zhangjie)
        except Exception:
            print('get_content failed')
    # 程序入口
    if __name__ == '__main__':
        url = 'http://www.quanshuwang.com/list/3_1.html'
        # 创建进程池
        pool = Pool()
        # 启动程序
        get_booklist(url)

    控制台输出

    正在下载: 章 节目录 第三十四章 不眠的天堂
    正在下载: 章 节目录 第四十四章 :耳光
    正在下载: 章 节目录 第046章 找到变异元晶
    正在下载: 章节目录 第二十八章 修路优惠
    正在下载: 章 节目录 第四十五章 :憋屈
    正在下载: 章 节目录 第047章 至宝得手
    正在下载: 章节目录 第二十九章 猜鱼
    正在下载: 章 节目录 第048章 凤凰涅槃,浴火重生。
    正在下载: 章节目录 第三十章 养猪场
    正在下载: 章 节目录 第四十六章 :酣畅淋漓
    正在下载: 章 节目录 第049章 上等天赋资质
    正在下载: 章节目录 第三十一章 上鬼身
    正在下载: 章 节目录 第050章 元力神兵
    正在下载: 章 节目录 第四十七章 :舵主之位
    正在下载: 章 节目录 第三十五章 黑暗
    正在下载: 章节目录 第三十二章 吓死马有钱
    正在下载: 章 节目录 第三十六章 商议
    正在下载: 章 节目录 第051章 天级上品龙隐术
    正在下载: 章 节目录 第三十七章 寻觅
    正在下载: 章节目录 第三十三章 再遇李三
    正在下载: 章节目录 第三十四章 借了一百万

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

     done。

  • 相关阅读:
    【模版 Luogu P3808/P3796/P5357】AC自动机(简论)
    2019暑假 诸暨海亮集训游记
    【数据结构模版】可持久化线段树 && 主席树
    2019浙江集训——待整理的知识点、博客和题解
    浅谈树链剖分 F&Q
    Luogu P1967 NOIP2013 货车运输
    最小树形图(朱刘算法)--学习笔记
    Luogu P43916 图的遍历
    pytest扫盲12--assert断言
    pytest扫盲11--xfail参数详解
  • 原文地址:https://www.cnblogs.com/nmsghgnv/p/11327680.html
Copyright © 2011-2022 走看看