[Python] 纯文本查看 复制代码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('小说下载完成!')