Python 小说小偷程序 ChatGPT修改

根据GPT提示修改的代码,没有添加协成,也没有多进程,多线程,下载一个小说运行速度也不慢,不容易出错!

import requests
from bs4 import BeautifulSoup
# 设置请求头
headers = {    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'}
# # 目标网站
ids = "15785"  #小说ID,可修改为你想下载的书
url = 'http://www.qiuyelou.net/'+ids+'/'
# 发送请求
response = requests.get(url, headers=headers)
response.encoding = "utf-8"
# 解析网页
soup = BeautifulSoup(response.text, 'html.parser')
# 获取小说标题
title = soup.find('div', class_='title').h1.text   #//*[@id="main"]/div[2]/div[1]/h1
print('正在下载小说:', title)
# 获取章节链接  //*[@id="novel15785"]/dl/dd[2]/a
chapter_urls = []
chapter_list = soup.find_all('dd')[0].find_all('a')
for chapter in chapter_list:   
    chapter_url = 'http://www.qiuyelou.net/'+ids+'/' + chapter['href']    
    chapter_urls.append(chapter_url)
# 下载每一章节的内容
print(chapter_urls)
for chap_url in chapter_urls:    
    # 发送请求    
    chapter_response = requests.get(chap_url, headers=headers)
    chapter_response.encoding = "utf-8"   
    # 解析网页    
    chapter_soup = BeautifulSoup(chapter_response.text, 'html.parser')    
    # 获取章节标题和内容
    chapter_title = chapter_soup.find('div', class_='title').h1.text 
    idclass = chap_url.split("/")[-1].replace(".html","")
    print(idclass)
    chapter_content = chapter_soup.find('div', id='content'+idclass+'').text
    # 写入文件
    with open("缓存/"+ title + '.txt', 'a', encoding='utf-8') as f:        
        f.write(chapter_title + '\n\n')        
        f.write(chapter_content + '\n\n')    
        print('已下载:', chapter_title)
print('小说下载完成!')
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

qq_24823461

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值