Python-简单小说爬虫(以《天官赐福》为例)

目录

准备

获取Cookie等

获取目录标签

获取内容标签

代码

视频讲解

结果

参考


准备

获取Cookie等

获取目录标签

li标签里面包含a或b标签,章节的链接是小说的url加一个整数100536开始,依次递增,文章的标题可以通过字符串查找得到。

获取内容标签

id为nr1的div块的内容就是本章小说内容

代码

"""
--coding:utf-8--
@File: __init__.py.py
@Author:frank yu
@DateTime: 2021.01.06 9:24
@Contact: frankyu112058@gmail.com
@Description:
"""
import random
import time

import requests
from bs4 import BeautifulSoup

tianguan = 'https://www.kunnu.com/tianguancifu/'
headers = {
    'User-Agent': 'Mozilla/5.0 ArchLinux (X11; U; Linux x86_64; en-US) AppleWebKit/534.30 (KHTML, like Gecko) '
                  'Chrome/12.0.742.100',
    'Cookie': '_ga=GA1.2.635907381.1607343598; _gid=GA1.2.910170177.1609896386; _gat_gtag_UA_16539659_3=1',
    'Connection': 'keep-alive',
    'referrer': 'https://www.kunnu.com/tianguancifu/'
}


# 获取目录
def get_contents(url, headers, start):
    res = requests.get(url, headers=headers, timeout=30)
    res.encoding = res.apparent_encoding
    soup = BeautifulSoup(res.text, 'html.parser')
    contents = soup.find_all('li')
    contents.pop(0)
    contents.pop(0)
    res = []
    for con in contents:
        link = f'{url}{start}.htm'
        begin = str(con).find('">') + 2
        end = str(con).find("</a>") if str(con).find("</b>") == -1 else str(con).find("</b>")
        title = '第' + str(con)[begin:end].replace('、', '章')
        res.append((link, title))
        start += 1
    # for i in res:
    #     print(i)
    # print(len(res))
    return res


# 爬取小说
def get_novel(contents, name):
    name = name + '_爬取.txt'
    with open(name, 'w', encoding='utf-8') as novel:
        for con in contents:
            link, title = con
            while True:
                try:
                    res = requests.get(link, headers=headers, timeout=60)
                    res.encoding = 'utf-8'
                    soup = BeautifulSoup(res.text, 'html.parser')
                    txt = soup.find(name='div', attrs={'id': 'nr1'})
                    novel.write('\n' + title + '\n')
                    novel.write(txt.text)
                    print(title + ' 已完成')
                    time.sleep(random.random() + 1)
                    break
                except Exception as e:
                    print(e)
                    time.sleep(random.random() + 5)
    print('全部爬取完毕, 生成' + name)
    return name


# 文本处理
def deal(name):
    new_name = name.split('_')[0] + '.txt'
    with open(new_name, encoding='utf-8', mode='w') as newfile:
        with open(name, encoding='utf-8', mode='r') as file:
            for line in file:
                index = line.find('鲲')
                if index != -1:
                    # print(index)
                    newfile.write(line[:index - 1])
                    continue
                newfile.write(line)
    return new_name


if __name__ == '__main__':
    con = get_contents(tianguan, headers, 100536)
    novel = get_novel(con, '天官赐福')

爬取之后可以进行一些文本处理等

视频讲解

《天官赐福》,给爷爬!!!(简单爬虫实战)

结果

我将txt文本导入了小说阅读APP(咪咕阅读),没有问题。

目录识别
小说阅读

-----------------------2021年06月22日更新--------------------

这个网站改了下,以上代码不行了,大家可以试试笔趣阁

参考

requests

BeautifulSoup

更多python相关内容:【python总结】python学习框架梳理

有问题请下方评论,转载请注明出处,并附有原文链接,谢谢!如有侵权,请及时联系。如果您感觉有所收获,自愿打赏,可选择支付宝18833895206(小于),您的支持是我不断更新的动力。

  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lady_killer9

感谢您的打赏,我会加倍努力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值