爬取笔趣阁全站小说

爬取笔趣阁全站小说,注意爬取的顺序。

import requests as r
from lxml import etree

class Pavilion:

    @classmethod
    def get_book(cls):
        url = "http://www.xbiquge.la/xiaoshuodaquan/"
        res = r.request(method="GET",url=url)
        str_html = res.text
        ele = etree.HTML(str_html)
        book_urls = ele.xpath("//div[@class='novellist']/ul/li/a/@href")
        return book_urls

    @classmethod
    def get_chapter(cls):
        book_urls = cls.get_book()
        for book_url in book_urls:
            res_html = r.request(method="GET",url=book_url).content.decode()
            ele = etree.HTML(res_html)
            book_name = ele.xpath("//h1/text()")[0]  # 每本书的书名
            book_chapter_urls = ele.xpath("//div[@id='list']/dl/dd/a/@href")
            cls.get_content(book_chapter_urls,book_name)

    @classmethod
    def get_content(cls,book_chapter_urls,book_name):
        for book_chapter_url in book_chapter_urls:
            print("换章节啦++++++++++++++++++++++")
            res_html = r.request(method="GET",url="http://www.xbiquge.la/"+book_chapter_url).content.decode()
            ele = etree.HTML(res_html)
            chapter_content = ele.xpath("//div[@id='content']/text()")
            book_chapter_name = ele.xpath("//h1/text()")[0]
            print(book_chapter_name)
            with open("crawler_demo\crawler_three\\books\\"+book_name + ".txt", "a+", encoding="utf-8") as w:
                w.write(book_chapter_name+"\n")
                for i in chapter_content:
                    w.write(i)
                w.write("\n\n")
if __name__ == '__main__':
    Pavilion.get_chapter()

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值