
在 Python 爬虫应用场景中,小说爬取是入门级且极具实用性的实战案例。《斗罗大陆》作为经典网络小说,章节数量多、内容结构清晰,非常适合用来讲解爬虫的核心逻辑与落地实现。本文将从需求分析、技术选型、代码实现到数据合并,完整拆解如何自动化抓取《斗罗大陆》全章节内容,并将分散的章节合并为完整的 TXT 文件,帮助你掌握爬虫开发的核心思路与实操技巧。
python
运行
import requests
from bs4 import BeautifulSoup
import time
import os
# 配置项
# 替换为实际的《斗罗大陆》章节列表页URL(需选择稳定的站点)
BASE_URL = "https://example.com/douluo/"
# 请求头,模拟浏览器访问
HEADERS = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
"Referer": BASE_URL,
"Accept-Encoding": "gzip, deflate",
"Accept-Language": "zh-CN,zh;q=0.9"
}
# ===== 新增代理配置 =====
proxyHost = "www.16yun.cn"
proxyPort = "5445"
proxyUser = "16QMSOML"
proxyPass = "280651"
# 构建代理字典(同时支持http和https)
proxies = {
"http": f"http://{proxyUser}:{proxyPass}@{proxyHost}:{proxyPort}",
"https": f"https://{proxyUser}:{proxyPass}@{proxyHost}:{proxyPort}"
}
# 保存路径
SAVE_DIR = "douluo_novel"
# 最终合并后的文件名
FINAL_FILE = "斗罗大陆-全本.txt"
def init_dir():
"""初始化保存目录"""
if not os.path.exists(SAVE_DIR):
os.makedirs(SAVE_DIR)
# 清空之前的临时文件(如果有)
if os.path.exists(os.path.join(SAVE_DIR, FINAL_FILE)):
os.remove(os.path.join(SAVE_DIR, FINAL_FILE))
def get_chapter_list():
"""爬取章节列表,返回[(章节标题, 章节URL), ...]"""
chapter_list = []
try:
# ===== 新增代理参数 proxies=proxies =====
response = requests.get(BASE_URL, headers=HEADERS, proxies=proxies, timeout=10)
response.encoding = response.apparent_encoding # 自动识别编码
soup = BeautifulSoup(response.text, "html.parser")
# ******** 关键解析逻辑(需根据目标站点调整)********
# 示例:假设章节列表在class为"chapter-list"的ul标签下,每个章节是li>a标签
chapter_items = soup.find("ul", class_="chapter-list").find_all("li")
for item in chapter_items:
a_tag = item.find("a")
if a_tag:
chapter_title = a_tag.get_text().strip() # 章节标题
chapter_url = a_tag.get("href") # 章节URL
# 拼接完整URL(如果是相对路径)
if not chapter_url.startswith("http"):
chapter_url = BASE_URL + chapter_url
chapter_list.append((chapter_title, chapter_url))
print(f"成功获取{len(chapter_list)}个章节")
return chapter_list
except Exception as e:
print(f"获取章节列表失败:{e}")
return []
def get_chapter_content(chapter_url):
"""爬取单个章节内容,返回纯文本内容"""
try:
# 增加随机延迟,避免请求过快触发反爬
time.sleep(0.5)
# ===== 新增代理参数 proxies=proxies =====
response = requests.get(chapter_url, headers=HEADERS, proxies=proxies, timeout=10)
response.encoding = response.apparent_encoding
soup = BeautifulSoup(response.text, "html.parser")
# ******** 关键解析逻辑(需根据目标站点调整)********
# 示例:章节内容在class为"content"的div标签下
content_tag = soup.find("div", class_="content")
if not content_tag:
return ""
# 清理无关标签(如广告、导航)
for unwanted in content_tag.find_all(["script", "style", "div", "p"], class_=["ad", "nav"]):
unwanted.extract()
# 提取纯文本并格式化(去除多余空格、换行)
content = content_tag.get_text().strip()
# 替换多个换行为单个换行,保证格式整洁
content = "\n".join([line.strip() for line in content.split("\n") if line.strip()])
return content
except Exception as e:
print(f"爬取章节失败 {chapter_url}:{e}")
return ""
def save_chapter(chapter_title, chapter_content, index):
"""保存单个章节到总文件"""
final_path = os.path.join(SAVE_DIR, FINAL_FILE)
# 追加写入,编码为utf-8避免中文乱码
with open(final_path, "a", encoding="utf-8") as f:
# 写入章节标题(带序号)
f.write(f"===== {index}. {chapter_title} =====\n\n")
# 写入章节内容
f.write(chapter_content)
# 章节间分隔
f.write("\n\n")
print(f"已保存:{index}. {chapter_title}")
def main():
"""主函数:爬取+合并全本"""
# 初始化目录
init_dir()
# 获取章节列表
chapter_list = get_chapter_list()
if not chapter_list:
print("未获取到章节列表,程序退出")
return
# 遍历章节,逐个爬取并保存
for index, (title, url) in enumerate(chapter_list, 1):
content = get_chapter_content(url)
if content:
save_chapter(title, content, index)
else:
print(f"跳过空章节:{title}")
print(f"\n爬取完成!全本已保存至:{os.path.join(SAVE_DIR, FINAL_FILE)}")
if __name__ == "__main__":
main()find()/find_all()的参数(如 class 名称、标签类型);utf-8编码解决中文乱码问题;代码中标记********的部分是需要你根据实际爬取站点调整的核心解析逻辑,操作步骤如下:
time.sleep(0.5),避免短时间内大量请求触发站点反爬机制,可根据站点宽松程度调整延迟时间(0.5-2 秒);response.apparent_encoding自动识别页面编码,解决中文乱码问题;运行代码后,会在当前目录下生成douluo_novel文件夹,其中的《斗罗大陆 - 全本.txt》即为合并后的完整小说文件。打开文件可看到:
plaintext
===== 1. 第一章 斗罗世界 =====
唐门外门弟子唐三,因偷学内门绝学为唐门所不容,跳崖明志时却发现没有死,反而以另外一个身份来到了另一个世界,一个属于武魂的世界,名叫斗罗大陆。
这里没有魔法,没有斗气,没有武术,却有神奇的武魂。每个人在六岁的时候,都会在武魂殿中令武魂觉醒。武魂有动物,有植物,有器物,武魂可以辅助人们的日常生活。而其中一些特别出色的武魂却可以用来修炼并进行战斗,这个职业,是斗罗大陆上最为强大也是最荣耀的职业——魂师。
===== 2. 第二章 武魂觉醒 =====
...所有章节按顺序排列,内容无多余广告、格式整洁,实现了 “自动抓取 + 合并” 的核心目标。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。