python小说爬虫源代码

news/2024/7/19 11:57:40 标签: 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))


http://www.niftyadmin.cn/n/5120343.html

相关文章

container_of解析及应用

container_of是一个C语言中比较少见,但实际经常用到的宏,在Linux kernel中也有大范围的应用。

腻子粉用不好,会开裂,发霉

1,因为很多腻子,都是包给装修公司或者半包出去      由于利益的原因,会直接给你用几块钱一袋的,没有任何执行标准      同时为了施工方便,会加入大量801胶水,      因为腻子便宜,质量不好      就得加70%的水和30%的胶,便宜的腻子和801胶水加在一起      肯…

vim创建文件

vim 是一个查看文本的编译器 现在想使用vim 创建1个文件 可以在vim 后面跟一个自己创建的文件,这个文件之前不存在。使用vim 就相当于打开一个空的文件 1 vim test.sh 这里test.sh 是自己创建的 2点击 insert 进入编译模式 在里面输入内容 ,随便…

图像的特征点描述与提取

一、说明 特征点算法是图像处理中主要算法之一,它是图像物体匹配的关键步骤,因此,是个极其重要的题目,至今依旧研究不断,本篇讲述历年来学者在领域研究的突出贡献,即六种不同的特征点提取办法,供…

RetentionPolicy枚举类

包名package java.lang.annotation 作用 注释保留策略。此枚举类型的常量描述用于保留注释的各种策略。它们被使用与{ Retention}元注释类型一起指定注释要保留多长时间。 属性 SOURCE编译器将丢弃注释。CLASS注释将由编译器记录在类文件…

非父子组件通信-发布订阅模式

发布订阅模式其实与vue无关,完全是ES6的代码,但是它可以通过这种模式实现非父子组件的通信 store.js文件 首先创建一个store.js文件,用于提供发布与订阅方法 export default {datalist: [], //存放带一个参数的函数集合//订阅subscribe(fu…

C# 使用waveIn实现声音采集

文章目录 前言一、需要的对象及方法二、整体流程三、关键实现1、使用Thread开启线程2、TaskCompletionSource实现异步3、将指针封装为Stream 四、完整代码1.接口2.具体实现 五、使用示例方式一方式二 总结 前言 之前实现了《C 使用waveIn实现声音采集》,后来C#项目…

MySQL - mvcc

mvcc 是什么? MVCC(多版本并发控制)是一种数据库并发控制机制,旨在提高数据库的并发性,避免锁定操作,从而减少等待和提高性能。MVCC 主要解决数据库读写操作之间的线程安全问题。 MVCC 主要有两种读取数据…