欢迎大家访问我的网站!
图片名称

奇书网小说下载

iwfacn2025-01-23 17:54:1643免费资源免费教程

奇书网小说下载


# -*- 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)

转载声明:本站发布文章及版权归原作者所有,转载本站文章请注明文章来源!

本文链接:http://www.iwfacn.com/728.html

图片名称
热门文章
随机文章
热门标签
侧栏广告位
图片名称
图片名称