# -*- coding: utf-8 -*-
from bs4 import BeautifulSoup
import requests
import os
# 保存目录
save_dir = "科幻灵异"
# 如果保存目录不存在,则创建目录
if not os.path.exists(save_dir):
os.makedirs(save_dir)
# 首页地址(不同类别更换后缀即可)
base_url = "https://www.qishu66.com/kehuan/"
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36'
}
response = requests.get(base_url, headers=headers)
# 自动检测编码
response.encoding = response.apparent_encoding
# 使用 BeautifulSoup 解析 HTML
soup = BeautifulSoup(response.text, 'lxml')
# 更清晰地打印 HTML
# print(soup.prettify())
# 总页数
element = soup.select_one('#splitpage > span:nth-child(1) > strong:nth-child(2)')
pages = int(element.text.strip())
print(f"总页数:{pages}")
# 通过链接下载文件
def downFile(url):
try:
# 取文件名
file_name = url.split("/")[-1]
# 获取文件流
response = requests.get(url, headers=headers, stream=True)
# 不是200响应则报错
response.raise_for_status()
# 拼接保存文件
save_path = os.path.join(save_dir, file_name)
# 打开文件并保存
with open(save_path, 'wb') as f:
for chunk in response.iter_content(chunk_size=8192):
if chunk:
f.write(chunk)
except BaseException as e:
print(f"下载出错: {e}")
# 获取下载链接
def downLoad(down_load_url):
response = requests.get(down_load_url, headers=headers)
# 自动检测编码
response.encoding = response.apparent_encoding
# 使用 BeautifulSoup 解析 HTML
soup = BeautifulSoup(response.text, 'lxml')
# 获取第一个下载地址
down_element = soup.select('.downlist')
down_a_tag = down_element[0].find('a')
down_file_url = down_a_tag['href']
print(f"文件下载地址:{down_file_url}")
downFile(down_file_url)
# 打开下载页
def downLoadUrl(down_url):
response = requests.get(down_url, headers=headers)
# 自动检测编码
response.encoding = response.apparent_encoding
# 使用 BeautifulSoup 解析 HTML
soup = BeautifulSoup(response.text, 'lxml')
# 获取下载页的链接
downlinks_element = soup.select('.downlinks')
first_a_tag = downlinks_element[0].find('a') # 获取第一个 a 标签
href = first_a_tag['href']
href = f"https://www.qishu66.com{href}"
print(f"下载地址页:{href}")
downLoad(href)
# 循环所有页
def get_pagination_links(child_url):
print(f"当前页面地址:{child_url}")
response = requests.get(child_url, headers=headers)
response.encoding = response.apparent_encoding
soup = BeautifulSoup(response.text, 'lxml') # 每次解析新的页面
slistElement = soup.select('.slist')
# 获取每页列表的a标签
for div in slistElement:
first_a_tag = div.find('a')
if first_a_tag:
href = first_a_tag['href'] # 获取 a 标签的 href 属性
base_down_url = f"https://www.qishu66.com{href}"
downLoadUrl(base_down_url)
else:
print(f"div 中未找到 a 标签")
for page in range(1, pages + 1):
# 构造当前页的链接地址
if page > 1:
child_page_url = f"{base_url}index_{page}.html"
else:
child_page_url = base_url
get_pagination_links(child_page_url)
奇书网小说下载
转载声明:本站发布文章及版权归原作者所有,转载本站文章请注明文章来源!
相关推荐
- 2025-04-17零基础少年象棋课,高清视频
- 2025-04-17高一下学期3月或者4月试卷,省份如图
- 2025-04-17少儿趣味历史知识动画《小马可波罗历险记 MarcoPolo (视频+音频) 》
- 2025-04-17凯叔讲故事《三国里的博物学》
- 2025-04-17【赚钱必看】真实有效391套商业模式,年入几十到几百万案例解析(视频+图文)
- 2025-04-17水木Deepseek系列( 视频课件俱全)
- 2025-04-172020Python开发基础班+就业班视频教程
- 2025-04-17每日荐书丨04月15日
- 2025-04-17做科研的大师兄
- 2025-04-17主播线上课直播万能公式(主要从人群进行讲解)
- 热门文章
- 随机文章
-
- Python学习:为什么在使用 schedule 库执行定期任务时需要延时?
- Python学习:为什么我在 Windows 10 上安装 uWSGI 时卡住了?
- Python学习:Excel 文件无法识别格式怎么办?
- Python学习:如何使用 Python 生成指定范围内指定数量的随机浮点数?
- Python学习:如何从两个数据结构中提取特定信息并将其组合成新的数据结构?
- Python学习:如何使用 Python 生成器表达式优化连续子数组查找算法?
- Python学习:如何解决 Pandas 读取 XLSX 文件出现的“Excel 文件格式无法确定”错误?
- Python学习:`-e` 或 `--editable` 选项如何提升 pip install?
- Python学习:如何在 Python 中优雅地导入上一级模块?
- Python学习:pydantic 库 validator 的 per 参数执行顺序异常:为什么设置 per 为 True 后,验证方法顺序并没有改变?
- 侧栏广告位