import requests from bs4 import BeautifulSoup

发送HTTP GET请求并获取网页内容

url = "https://www.biquge66.net/book/1433/" response = requests.get(url)

使用BeautifulSoup解析网页内容

soup = BeautifulSoup(response.content, "html.parser")

找到所有包含章节链接的
元素

chapter_lists = soup.find_all("div", class_="flex flex-between book-info-main")

遍历每个
元素,然后遍历其中的每个章节链接

for chapter_list in chapter_lists: for chapter in chapter_list.find_all("a", rel="chapter"): chapter_title = chapter.text # 获取章节标题 chapter_url = "https://www.biquge66.net" + chapter["href"] # 构建完整的章节链接

    # 发送HTTP GET请求以获取第一页章节内容
    chapter_response = requests.get(chapter_url)
    chapter_soup = BeautifulSoup(chapter_response.content, "html.parser")

    # 找到包含第一页章节内容的<div>元素
    chapter_content = chapter_soup.find("div", id="booktxt")

    # 提取第一页章节内容文本并打印出来
    if chapter_content:
        chapter_text = chapter_content.text

        # 检查是否有第二页
        chapter_url2 = chapter_url.replace(".html", "_2.html")
        chapter_response2 = requests.get(chapter_url2)
        chapter_soup2 = BeautifulSoup(chapter_response2.content, "html.parser")
        chapter_content2 = chapter_soup2.find("div", id="booktxt")

        # 如果有第二页,拼接第一页和第二页的内容
        if chapter_content2:
            chapter_text2 = chapter_content2.text
            chapter_text += chapter_text2

        print(f"{chapter_title}:

{chapter_text} ") else: print(f"无法获取章节内容:{chapter_title} ")

Python爬虫:获取小说网站章节内容并拼接两页内容

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

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