跳至正文
-
Subscribe to our newsletter & never miss our best posts. Subscribe Now!
BG6RSH's 随记.

我们总是在感叹昨天,忽视今天,幻想着明天中渡过,且过且珍惜.....

BG6RSH's 随记.

我们总是在感叹昨天,忽视今天,幻想着明天中渡过,且过且珍惜.....

  • 首页
  • 论坛
  • 随记
  • 编程
  • 软件
  • 留言
  • 首页
  • 论坛
  • 随记
  • 编程
  • 软件
  • 留言
关

搜索

  • https://www.facebook.com/
  • https://twitter.com/
  • https://t.me/
  • https://www.instagram.com/
  • https://youtube.com/
Subscribe
编程

用PYTHON爬网络小说源代码

作者 admin
2022年10月12日 1 分钟阅读
0

该代码基本上可下载绝大部分小说网站的小说,有问题也只需要调整小部分的代码。
1、修改一下url变量,改为小说目录列表页面。
2、修改第34行“for i in range(20, len(tag_h3)):”20这个值,跟踪进去可调整从第几章开始下载。
3、第36行也得改成网站首地址。

Python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# -*- coding: utf-8 -*-
"""
Spyder Editor
 
This is a temporary script file.
"""
 
from bs4 import BeautifulSoup
import requests
import time
import os
import threading
 
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.61 Safari/537.36'}
saveDir = 'd:/鸿天神尊.txt'  
url = 'https://www.tycqzw.com/62_62436/'  
 
 
# 获得所有章节的URL
def get_topic(url):
    headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.61 Safari/537.36'}
    
    
 
    response = requests.get(url, headers=headers)
    response.encoding = response.apparent_encoding
 
    soup = BeautifulSoup(response.content, 'html.parser')
    tag_h3 = soup.find_all('dd')
    
    f = open(saveDir, mode='a', encoding='utf8')
    
    try:
        for i in range(520, len(tag_h3)):
            topic_url = tag_h3[i].find('a').get('href')  # 获得主题链接
            topic_url = 'https://www.tycqzw.com/'+topic_url   # 主题页面 url
            txt = tag_h3[i].find('a').get_text() # 主题名称
            print(topic_url +' ' + txt)
            f.write(get_txt(topic_url, txt))
    finally:
        f.close()
        
 
# 下载链接页面中的文本内容
def get_txt(url, txt):
    response = requests.get(url, headers=headers)
    response.encoding = response.apparent_encoding
    soup = BeautifulSoup(response.content, 'html.parser')
    tag_txt = soup.find('div', id='content').get_text() #(separator='\n')
    i = tag_txt.find('(本章完)')
    return txt+'\n'+tag_txt[:i]
if __name__ == '__main__':
    print('===== 主程序开始 =====')
    start = time.perf_counter()
    get_topic(url)
    tt = time.perf_counter() - start
    print('===== 主程序结束,用时 {:.2f}s ====='.format(tt))

 

标签:

python爬虫
作者

admin

关注我
其他文章
上一个

QT5.15.2搭建ANDROID编译环境

下一个

你的 Python 环境或安装版没有 spyder‑kernels 模块或没有安装正确的版本 (>=

暂无评论!成为第一个。

发表回复 取消回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理。

4条垃圾评论已被Akismet阻挡 (在新标签页中打开)

近期文章

  • www.tansoo.cn网站空间迁移至armbian主机
  • 在QT6中安装qtmqtt、qtopcua控件的方法
  • 你的 Python 环境或安装版没有 spyder‑kernels 模块或没有安装正确的版本 (>=
  • 用PYTHON爬网络小说源代码
  • QT5.15.2搭建ANDROID编译环境

近期评论

  • gg 发表在《Traccar服务软件高德地图适配版docker镜像安装教程》
  • admin 发表在《Traccar服务软件高德地图适配版docker镜像安装教程》
  • /api/positions/里addres 为什么是null啊? 发表在《Traccar服务软件高德地图适配版docker镜像安装教程》
  • gg 发表在《Traccar服务软件高德地图适配版docker镜像安装教程》
  • 9394 发表在《Traccar服务软件高德地图适配版docker镜像安装教程》

归档

  • 2025 年 6 月
  • 2023 年 6 月
  • 2022 年 10 月
  • 2021 年 5 月
  • 2021 年 4 月
  • 2020 年 11 月
  • 2020 年 10 月
  • 2020 年 6 月
  • 2019 年 11 月
  • 2019 年 8 月
  • 2019 年 6 月
  • 2019 年 1 月
  • 2018 年 12 月
  • 2018 年 11 月
  • 2017 年 9 月
  • 2016 年 7 月
  • 2016 年 5 月
  • 2015 年 12 月
  • 2015 年 4 月
  • 2015 年 1 月
  • 2014 年 12 月
  • 2014 年 11 月
  • 2014 年 10 月
  • 2014 年 9 月
  • 2014 年 8 月
  • 2014 年 7 月
  • 2014 年 6 月
  • 2014 年 5 月
  • 2013 年 10 月
  • 2013 年 9 月
  • 2013 年 8 月
  • 2013 年 7 月
  • 2013 年 6 月
  • 2013 年 5 月
  • 2013 年 4 月
  • 2013 年 3 月
  • 2013 年 2 月
  • 2013 年 1 月
  • 2012 年 12 月
  • 2012 年 11 月
  • 2012 年 10 月
  • 2012 年 6 月
  • 2012 年 3 月
  • 2011 年 8 月
  • 2011 年 5 月
  • 2011 年 4 月
  • 2011 年 3 月
  • 2011 年 1 月
  • 2010 年 8 月
  • 2010 年 6 月
  • 2010 年 4 月
  • 2010 年 3 月
  • 2010 年 2 月
  • 2010 年 1 月
  • 2009 年 12 月
  • 2009 年 11 月

分类

  • 编程
  • 软件
  • 随记

其他操作

  • 注册
  • 登录
  • 条目 feed
  • 评论 feed
  • WordPress.org

标签

aliyun Android CRC DBGridEh ddns Delphi EhLib ESP8266 GP328D gp338plus GP3688 Modbus phpbb python QT raspberry S7-SCL sapi speech SpVoice Synology traccar UART Unicode virtualbox vmware WGS84 wordpress ZeroTier 写频 动态IP 图片尺寸 地图 外挂编程 天气预报 导航 打印 扩频 拼音首字母 支票快打 树莓派 爬虫 百度地图 西门子PLC 车联网API

友情链接

  • 私有云论坛
  • 百度开放云
  • 主机管理
  • My Cloud
  • 智能家居技术论坛
  • 精灵云
  • 雅苑私站
Copyright 2026 — BG6RSH's 随记.. All rights reserved. Blogsy WordPress Theme