好友
阅读权限10
听众
最后登录1970-1-1
|
import requests
from bs4 import BeautifulSoup
url = "https://fanqienovel.com/page/7356041190682135577?enter_from=stack-room"
response = requests.get(url)
if response.status_code == 200:
content = response.text
soup = BeautifulSoup(content, "html.parser")
chapter_div = soup.find("div", {"class": "chapter"})
if chapter_div:
chapter_items = chapter_div.find_all("div", {"class": "chapter-item"})
for index, chapter_item in enumerate(chapter_items, start=1):
chapter_title_a = chapter_item.find("a", {"class": "chapter-item-title"})
chapter_title = chapter_title_a.text
chapter_link = chapter_title_a['href']
print(f"第{index}章:{chapter_title} - https://fanqienovel.com{chapter_link}")
# 获取章节内容
chapter_url = f"https://fanqienovel.com{chapter_link}"
chapter_response = requests.get(chapter_url)
if chapter_response.status_code == 200:
chapter_content = chapter_response.text
chapter_soup = BeautifulSoup(chapter_content, "html.parser")
chapter_text_div = chapter_soup.find("div", {"class": "muye-reader-content noselect"})
if chapter_text_div:
chapter_text = chapter_text_div.get_text(strip=True)
chapter_lines = chapter_text.splitlines()
cleaned_chapter_lines = [line.strip() for line in chapter_lines if line.strip()]
cleaned_chapter_text = "\n".join(cleaned_chapter_lines)
print(cleaned_chapter_text)
else:
print("未找到章节内容")
else:
print(f"请求失败,状态码:{chapter_response.status_code}")
else:
print("未找到分章节内容")
else:
print(f"请求失败,状态码:{response.status_code}")
获取的文字被隐藏了,有没有大佬指点一下 |
|
发帖前要善用【论坛搜索】功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。 |
|
|
|
|