找回密码
  注册[Register]
查看: 373|回复: 6

[python] Python某笔趣阁小说下载

[复制链接]
发表于 2021-5-14 08:28 | 显示全部楼层 |阅读模式
禁止求评分、诱导评分、互刷评分、互刷悬赏值,违规者封号处理。
禁止发布推广、邀请码、邀请链接、二维码或者有利益相关的任何推广行为。
所有非原创软件请发布在【精品软件区】,发帖必须按照本版块版规格式发帖。

今天尝试爬取某笔趣阁小说下载,有两个选项:1是自动下载全站完本小说,2是根据输入网址下载单本小说。
1.将小说按类别放在文件夹中;2.整本小说保存在一个txt文档,而非一个章节放一个txt文档,方便转到手机阅读。

本人还是新手,将学习的过程发出来,希望与大家共同进步,请各位大佬轻拍!!
运行界面如下:
11.png
22.png
代码如下:


[Python] 纯文本查看 复制代码
import requests
from lxml import etree
import time
import os
import random

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36'
}

filePath = '.\笔趣阁全站小说'

def getPageInfo(url):
    try:
        html = requests.get(url=url, headers=headers)
        html.encoding = 'utf-8'
        if html.status_code == 200:
            return html.text
        return None
    except Exception:
        return None

# 下载全站完本小说
def downloadAllNovels(html):
    seclector1 = etree.HTML(html)
    # 获取小说页面的页数
    page_list = seclector1.xpath('//div[@class="pagelink"]/a[@class="last"]/text()')[0]
    # 获取每一页的链接
    for i in range(1, int(page_list)):
        # 每一页的链接
        url_single_page = 'https://www.biquwx.la/wanjiexiaoshuo/' + str(i)
        content = getPageInfo(url_single_page)
        seclector2 = etree.HTML(content)

        # 每一本小说链接
        single_novels = seclector2.xpath('//*[@id="main"]/div[1]/ul/li')
        for single_novel in single_novels:
            # 链接
            single_novel_address = single_novel.xpath('span[2]/a/@href')[0]
            # 执行下载单本小说函数
            downloadSingleNovel(single_novel_address)

# 下载单本小说
def downloadSingleNovel(html):
    # 获取每一本小说的信息
    content = getPageInfo(html)
    seclector = etree.HTML(content)
    # 小说名
    novel_title = seclector.xpath('//*[@id="info"]/h1/text()')[0]
    # 小说作者
    novel_author = seclector.xpath('//*[@id="info"]/p[1]/text()')[0]
    # 小说类别
    novel_category = seclector.xpath('//*[@id="info"]/p[2]/text()')[0].split(':')[1]

    # 按小说类别建立文件夹
    filePath_novel_category = filePath + '/' + novel_category
    if not os.path.exists(filePath_novel_category):
        os.mkdir(filePath_novel_category)

    # 小说简介
    _novel_introduce = seclector.xpath('//*[@id="intro"]/p[1]/text()')
    novel_introduce = ''
    for _ in _novel_introduce:
        novel_introduce += _ + '\n'

    #  先将小说名、作者、简介写入txt文件,并保存到相应类别的文件夹中
    with open(filePath_novel_category + '/' + novel_title + '.txt', 'a+', encoding='utf-8') as f:
        f.write('    ' + novel_title + '\n\n')
        f.write('    ' + novel_author + '\n\n')
        # f.write('     ' + novel_category + '\n')
        f.write(novel_introduce + '\n')

    # 获取小说章节链接
    _novel_chapter_name_address = seclector.xpath('//*[@id="list"]/dl/dd')
    for _ in _novel_chapter_name_address:
        # 章节链接
        novel_chapter_address = html + _.xpath('a/@href')[0]

        # 获取每一章节的章节名及正文
        content3 = getPageInfo(novel_chapter_address)
        seclector4 = etree.HTML(content3)
        # 章节名
        novel_chapter_name = seclector4.xpath('//*[@id="wrapper"]/div[5]/div/div[2]/h1/text()')[0]
        # 每一章节的正文
        _single_chapter_content = seclector4.xpath('//*[@id="content"]/text()')
        single_chapter_content = ''
        for _ in _single_chapter_content:
            single_chapter_content += _

        with open(filePath_novel_category + '/' + novel_title + '.txt', 'a+', encoding='utf-8') as f:
            # 添加章节名
            f.write('    ' + novel_chapter_name)
            # 添加章节内容
            f.write('    ' + single_chapter_content)
        print('%s %s 下载完成' % (novel_title, novel_chapter_name))
    print('____________')
    print('%s 下载完成' % novel_title)

def main():
    # 创建文件夹:笔趣阁全站小说
    if not os.path.exists(filePath):
        os.mkdir(filePath)

    print('下载全站完本小说,请输入1;按需下载单本小说,请输入2')
    if input() == '1':
        url = 'https://www.biquwx.la/wanjiexiaoshuo/'
        html = getPageInfo(url)
        downloadAllNovels(html)
        time.sleep(random.uniform(0.05, 0.2))
    else:
        print('请输入需要下载的小说网址(目录页):')
        html = input()
        downloadSingleNovel(html)
        time.sleep(random.uniform(0.05, 0.2))

if __name__ == '__main__':
    main()

如何回报帮助你解决问题的坛友,一个好办法就是给对方加【热心值】和【牛币】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
发表于 2021-5-14 08:32 | 显示全部楼层
谢谢大佬分享
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【热心值】和【牛币】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 有用 没用

使用道具 举报

发表于 2021-5-14 08:55 | 显示全部楼层
谢谢@Thanks!
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【热心值】和【牛币】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 有用 没用

使用道具 举报

发表于 2021-5-14 09:09 | 显示全部楼层
6666
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【热心值】和【牛币】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 有用 没用

使用道具 举报

发表于 2021-5-14 09:10 | 显示全部楼层
6666666666
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【热心值】和【牛币】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 有用 没用

使用道具 举报

发表于 2021-5-14 10:00 | 显示全部楼层
多谢分享多谢分享
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【热心值】和【牛币】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 有用 没用

使用道具 举报

发表于 2021-5-14 10:25 | 显示全部楼层

感谢楼主的分享
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【热心值】和【牛币】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 有用 没用

使用道具 举报

您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

RSS订阅|手机版|小黑屋|广告投放|大牛论坛

GMT+8, 2024-5-13 17:37 , Processed in 0.049472 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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