由于该网站存在反爬虫机制,需要使用一些技巧来突破,以下是可能的解决方案:

  1. 伪装浏览器:在请求网页时,需要设置请求头部,模拟浏览器访问。可以使用requests库发送GET请求,设置headers参数即可。

  2. 解析HTML内容:可以使用BeautifulSoup库解析HTML内容,提取出小说章节的链接地址。

  3. 下载小说章节:对于每个小说章节的链接地址,使用requests库发送GET请求,获取小说内容,然后保存到本地文件中。

以下是可能的代码实现:

import os
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.36'
}

# 极品戒指小说的主页
url = 'https://www.biqugemm.com/13_13743/'

# 发送GET请求,获取HTML内容
response = requests.get(url, headers=headers)
response.encoding = 'utf-8'
html = response.text

# 解析HTML内容,提取小说章节的链接地址
soup = BeautifulSoup(html, 'html.parser')
chapters = soup.select('#list a')

# 保存小说章节到本地文件中
for chapter in chapters:
    chapter_url = url + chapter['href']
    chapter_title = chapter.text
    print('正在下载:', chapter_title)

    # 发送GET请求,获取小说内容
    response = requests.get(chapter_url, headers=headers)
    response.encoding = 'utf-8'
    chapter_content = response.text

    # 保存小说内容到本地文件中
    file_path = os.path.join('D:', '个人资料', chapter_title + '.txt')
    with open(file_path, 'w', encoding='utf-8') as f:
        f.write(chapter_content)

注意事项:

  1. 代码中的'utf-8'编码方式可能需要根据实际情况进行修改。

  2. 如果该网站更新了反爬虫机制,代码可能会失效。

Python爬取极品戒指小说,按章节保存到本地

原文地址: https://www.cveoy.top/t/topic/ovAI 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录