python小工具 爬取小说网站生成TXT文件

import requests
from requests_html import HTMLSession


def get_txt(url):
    session = HTMLSession()
    r = session.get('http://m.kanxiaoshuo8.com/{}'.format(url))
    r.encoding = 'utf-8'
    content = r.html.xpath("//div[@id='chaptercontent']", first=True).text
    title = r.html.xpath("/html/body/header/span", first=True).text
    title = title.replace(' ', ' ').replace('吞噬古帝', '').strip()
    print(title)
    content = content.replace('『加入书签,方便阅读』', '').strip()
    with open("test.txt", 'a+', encoding='utf-8') as file:
        file.write(title + "\n")
        file.write(content + "\n")
        file.close()
    # print(r.text)
    url = r.html.xpath("/html/body/p[2]/a[3]/@href", first=True)
    if url:
        get_txt(url)


if __name__ == '__main__':
    get_txt("77_77563/6521484.html")

只适用网址:http://m.kanxiaoshuo8.com/ 

改一下可以加多线程,把整个网站上的小说爬取下来

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值