python小说爬虫源代码

利用python来获取网络小说是非常方便的,编程也不难,稍微改一下代码内容,基本上网络小说都能抓取下来。留下源代码方便以后使用。

python 复制代码
# -*- coding: utf-8 -*-
"""
《武灵天下》网络小说爬虫.
"""

from bs4 import BeautifulSoup
import requests
import time
import os
from threading import Thread

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:\\武灵天下\\'  
url = 'http://www.yuyouge.com/book/39764/'   #《武灵天下》 目录页


# 获得所有主题的URL
def get_topic(url):
    id = 0
    threads = []
    
    response = requests.get(url, headers=headers)
    response.encoding = response.apparent_encoding

    soup = BeautifulSoup(response.content, 'html.parser')
    tag_lis = soup.find('ul', id='chapters-list').find_all('li')
    
    for tag_li in tag_lis:
        tag_a = tag_li.find('a')
        
        if tag_a:
            topic_url = tag_a.get('href')  # 获得主题链接
            topic_url = 'https://www.yuyouge.com'+topic_url   # 主题页面 url
            txt = str.replace(tag_li.find('a').get_text(), 'VIP_', '') # 主题名称
            txt = str.replace(txt, '正文_', '') # 主题名称
            if txt[0] == '第':
                print(topic_url +' ' + txt)
                id += 1
                # get_txt(id, topic_url, txt)
                thread = Thread(target=get_txt, args=(id, topic_url, txt)) # 线程调用文本下载函数,下载主题页面中的文本内容
                thread.start()
                threads.append(thread)
    print(id)         
    for t in threads:
        t.join()
        

# 下载链接页面中的文本内容
def get_txt(id, url, txt):
    #print(response.encoding)
    txt = str.replace(txt, '/', ' ')
    txt = str.replace(txt, '\\', ' ')
    txt_file = '%s%04d-%s.txt'%(saveDir, id, txt)
    # 如果目录不存在,就新建目录
    if not os.path.exists(saveDir):
        os.makedirs(saveDir)

    # 文件不存在就下载,否则跳过
    if not os.path.exists(txt_file):
        response = requests.get(url, headers=headers)
        response.encoding = response.apparent_encoding
        soup = BeautifulSoup(response.content, 'html.parser')
        tag_txt = soup.find('div', id='txtContent').get_text(separator='\n')
        # i = tag_txt.find('www.qianyege.com')
        # tag_txt = txt + tag_txt[i+16:]
        # print(tag_txt)
        with open(txt_file, mode='w', encoding='utf8') as f:
            size = f.write(tag_txt)
            f.close()
            print("Threads-(%d) : %s(%d Bytes)"%(id, txt_file, size))


if __name__ == '__main__':
    print('===== 主程序开始 =====')
    start = time.perf_counter()
    get_topic(url)
    # get_txt(12,'https://www.yuyouge.com/book/39764/135943672.html', '第2020章 如影随形')
    tt = time.perf_counter() - start
    print('===== 主程序结束,用时 {:.2f}s ====='.format(tt))
相关推荐
专注API从业者16 分钟前
《Go 语言高并发爬虫开发:淘宝商品 API 实时采集与 ETL 数据处理管道》
开发语言·后端·爬虫·golang
Humbunklung24 分钟前
PySide6 GUI 学习笔记——常用类及控件使用方法(常用类矩阵QRectF)
笔记·python·学习·pyqt
蹦蹦跳跳真可爱58936 分钟前
Python----深度学习(基于DNN的吃鸡预测)
python·深度学习·dnn
欧先生^_^1 小时前
Scala语法基础
开发语言·后端·scala
hu_yuchen1 小时前
C++:BST、AVL、红黑树
开发语言·c++
炯哈哈1 小时前
【上位机——MFC】视图
开发语言·c++·mfc·上位机
我也不曾来过11 小时前
继承(c++版 非常详细版)
开发语言·c++
JJ1M81 小时前
Git技巧:Git Hook,自动触发,含实战分享
git·python·自动化
拓端研究室TRL2 小时前
PyMC+AI提示词贝叶斯项目反应IRT理论Rasch分析篮球比赛官方数据:球员能力与位置层级结构研究
大数据·人工智能·python·算法·机器学习
purrrew2 小时前
【JAVA ee初阶】多线程(3)
java·开发语言