吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 3173|回复: 24
收起左侧

[Python 转载] 使用Python爬虫一键爬取XX阁小说

  [复制链接]
mingxin123 发表于 2022-8-2 12:25
本帖最后由 mingxin123 于 2022-8-3 09:04 编辑

超级简单爬取笔趣阁小说的Python代码,只需要一个Python环境就能运行

技术栈:requests,xpath

超级简单爬取笔趣阁小说的Python代码,只需要一个Python环境就能运行

技术栈:requests,xpath

直接上代码

import os

import requests
from lxml import etree

def download_txt(name):
    params = {
        "keyword": name
    }
    host = "https://www.1biqug.com"
    resp = requests.get("https://www.1biqug.com/searchbook.php", params=params)
    html = resp.content.decode()
    html = etree.HTML(html)
    ret_list = html.xpath("//li/span[@class='s2']/a/@href")
    detail_url = host + ret_list[0]
    resp = requests.get(detail_url)
    html = etree.HTML(resp.content.decode())
    ret_list = html.xpath("//div[@id='list']//dd//a/@href")
    print(ret_list)
    if not os.path.exists("./{}".format(name)):
        os.mkdir("./{}".format(name))
    for ret in ret_list[12:]:
        url = host + ret
        resp = requests.get(url)
        info = resp.content.decode()
        html = etree.HTML(info)
        title = html.xpath("//h1/text()")
        print(title[0])
        path = os.path.join(name, title[0] + ".html")
        path = path.replace("*", "")
        with open(path, 'w', encoding="utf8") as f:
            f.write(info)
    print(name, "下载完成了")

if __name__ == '__main__':
    story = input("请输入小说名")
    download_txt(story)


附上运行成功截图

1659488673.jpg



更新更新
有朋友说要把格式转成txt格式,这里更新一版结果是txt格式的代码,还是直接复制过去就可以用。
import os

import requests
from lxml import etree

def download_txt(name):
    params = {
        "keyword": name
    }
    host = "https://www.1biqug.com"
    resp = requests.get("https://www.1biqug.com/searchbook.php", params=params)
    html = resp.content.decode()
    html = etree.HTML(html)
    ret_list = html.xpath("//li/span[@class='s2']/a/@href")
    detail_url = host + ret_list[0]
    resp = requests.get(detail_url)
    html = etree.HTML(resp.content.decode())
    ret_list = html.xpath("//div[@id='list']//dd//a/@href")
    print(ret_list)
    if not os.path.exists("./{}".format(name)):
        os.mkdir("./{}".format(name))
    for ret in ret_list[12:]:
        url = host + ret
        resp = requests.get(url)
        info = resp.content.decode()
        html = etree.HTML(info)
        title = html.xpath("//h1/text()")
        path = os.path.join(name, title[0] + ".txt")
        path = path.replace("*", "")
        content = html.xpath("//div[@id='content']//text()")
        if os.path.exists("./{}/{}".format(name, title)):
            os.remove("./{}/{}".format(name, title))
        f_content = open(path, "a", encoding="utf-8")
        for con in content:
            if "chaptererror();" in con or "本站最新域名:" in con:
                break
            f_content.write(con + "\r\n")
        f_content.close()
        print(title[0])
    print(name, "下载完成了")

if __name__ == '__main__':
    story = input("请输入小说名:")
    download_txt(story)


附上运行成功截图
1659488019(1).png

正文内容

正文内容

免费评分

参与人数 5吾爱币 +5 热心值 +4 收起 理由
zhuce129 + 1 我很赞同!
zzhhmm + 1 + 1 热心回复!
Jackbboy + 1 + 1 用心讨论,共获提升!
petocu + 2 + 1 用心讨论,共获提升!
hacknday + 1 我很赞同!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

 楼主| mingxin123 发表于 2022-8-3 08:22
wenxin2150 发表于 2022-8-2 15:53
你这是直接将小说每一章节的网页下载下来吗?我还以为你是把小说内容爬取出来

比较懒哈哈哈
wangli123 发表于 2022-10-1 10:53
virsnow 发表于 2022-9-30 18:44
lanxiongmao2016 发表于 2022-9-19 11:14
想问下能否替换其他网站呢,意思是把某阁换成其他网站。
shiwowo 发表于 2022-8-23 22:56
到老做个成品吧 哈哈辛苦
communistzhao 发表于 2022-8-3 13:35
谢谢分享!好人一生平安!
随梦期初 发表于 2022-8-3 12:37
在合成一个txt文件感觉更好
Trowa520 发表于 2022-8-3 11:31
感谢分享
pangpang02 发表于 2022-8-3 11:08
好家伙再做一个合并功能就好了
 楼主| mingxin123 发表于 2022-8-3 08:27
kyle233 发表于 2022-8-2 15:40
感谢楼主,可以转成TXT格式吗

可以,我今天再更新一下
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-5-12 07:34

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表