文章目录
(示意图:网络爬虫工作原理)
一、为什么要爬小说?这5个理由说服你!
- 批量下载追更神器(不用每天手动刷新!)
- 制作个人电子书库(把喜欢的小说永久保存)
- 数据分析好素材(研究网文写作规律)
- 离线阅读大法(地铁没信号也能看!)
- 技术练手最佳场景(反爬机制相对简单)
二、实战准备(3分钟搞定环境)
# 必备三件套安装命令(在终端执行)
pip install requests beautifulsoup4 lxml
安装验证小技巧:
print("Hello 爬虫!")
能运行 → Python环境OK
遇到SSL错误?试试 pip install --upgrade certifi
三、手把手教学:爬取某小说网站实战
步骤1:锁定目标页面
以某小说网站为例(请自行替换合法网站):
base_url = "http://www.example.com/novel/123"
步骤2:伪装浏览器请求
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36',
'Referer': 'http://www.example.com/'
}
response = requests.get(base_url, headers=headers)
步骤3:解析章节列表(BeautifulSoup大显身手)
soup = BeautifulSoup(response.text, 'lxml')
chapters = soup.select('.chapter-list a') # 根据实际网站结构调整选择器
# 打印前5章测试
for chapter in chapters[:5]:
print(chapter['href'], chapter.text)
步骤4:内容抓取核心代码
def get_chapter_content(chapter_url):
res = requests.get(chapter_url, headers=headers)
soup = BeautifulSoup(res.text, 'lxml')
content = soup.find('div', class_='content').text # 根据实际结构调整
return content.strip()
步骤5:自动保存到本地
with open('novel.txt', 'a', encoding='utf-8') as f:
for index, chapter in enumerate(chapters):
content = get_chapter_content(chapter['href'])
f.write(f"\n\n第{index+1}章 {chapter.text}\n")
f.write(content)
print(f"已下载:第{index+1}章") # 进度提示
time.sleep(1) # 礼貌间隔
四、常见反爬破解技巧(亲测有效!)
1. 验证码拦截 → 降低请求频率
time.sleep(random.uniform(0.5, 2)) # 随机延迟更逼真
2. IP封禁 → 使用代理池
proxies = {
'http': 'http://10.10.1.10:3128',
'https': 'http://10.10.1.10:1080',
}
requests.get(url, proxies=proxies)
3. 动态加载内容 → Selenium模拟
from selenium import webdriver
driver = webdriver.Chrome()
driver.get(url)
content = driver.find_element_by_css_selector('.content').text
五、法律红线千万别踩!(超级重要)
- 检查robots.txt(在网站域名后加/robots.txt)
- 不要突破付费章节
- 控制请求频率(建议≥3秒/次)
- 抓取内容仅限个人使用
- 尊重网站版权声明
六、扩展升级玩法
- 自动推送到Kindle:用email模块发送mobi文件
- 更新监控脚本:用定时任务检查最新章节
- 小说词云分析:用jieba+wordcloud生成可视化
- 有声书转换:调用语音合成API
# 简单词云示例
from wordcloud import WordCloud
text = open('novel.txt', encoding='utf-8').read()
wc = WordCloud(font_path='msyh.ttc').generate(text)
wc.to_file('wordcloud.png')
七、常见报错解决方案
- 403 Forbidden → 更新User-Agent
- SSLError →
verify=False
(慎用!) - 编码错误 →
response.encoding = 'gbk'
- 超时问题 →
timeout=10
参数 - 元素找不到 → 检查选择器是否更新
八、最佳实践建议
- 使用面向对象编程封装爬虫类
- 添加异常处理机制
- 配置日志记录系统
- 重要数据定期备份
- 使用数据库存储(MySQL/MongoDB)
# MongoDB存储示例
from pymongo import MongoClient
client = MongoClient('localhost', 27017)
db = client['novel_db']
collection = db['chapters']
collection.insert_one({'title': chapter_name, 'content': content})
最后说两句(必看!)
虽然技术无罪,但使用需谨慎!建议从学习角度出发,不要用于商业用途。遇到付费内容请支持正版,好的创作环境需要大家共同维护~
(注:本文示例代码需替换真实网址后使用,仅供学习交流)