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))
相关推荐
databook7 小时前
Manim实现脉冲闪烁特效
后端·python·动效
程序设计实验室7 小时前
2025年了,在 Django 之外,Python Web 框架还能怎么选?
python
倔强青铜三9 小时前
苦练Python第46天:文件写入与上下文管理器
人工智能·python·面试
用户25191624271112 小时前
Python之语言特点
python
刘立军12 小时前
使用pyHugeGraph查询HugeGraph图数据
python·graphql
数据智能老司机16 小时前
精通 Python 设计模式——创建型设计模式
python·设计模式·架构
数据智能老司机17 小时前
精通 Python 设计模式——SOLID 原则
python·设计模式·架构
c8i18 小时前
django中的FBV 和 CBV
python·django
c8i18 小时前
python中的闭包和装饰器
python
这里有鱼汤1 天前
小白必看:QMT里的miniQMT入门教程
后端·python