首先明确页面层级结构,再通过 requests+beautifulsoup或 scrapy 框架逐层抓取。1. 分析 URL 规律和 html 结构;2. 用 requests 获取列表页并提取详情链接;3. 遍历链接解析详情内容;4. Scrapy 中使用 yield Request 实现多级跳转;5. 注意设置请求头、间隔、异常处理与反爬策略。

抓取多级页面是 python 爬虫中常见的需求,比如从列表页进入详情页、从一级分类跳转到二级分类等。要实现多层级网页数据抓取,关键在于理清页面之间的跳转逻辑,并逐层提取所需信息。下面介绍几种常用方法和实现思路。
1. 明确页面层级结构
在开始 编码 前,先分析目标网站的页面结构。典型的多级结构如下:
- 第一层:主页面或分类列表(如新闻列表)
- 第二层:详情页面链接(如单条新闻页)
- 第三层(可选):评论页、作者页等更深层内容
通过 浏览器 开发者 工具 查看每层页面的 URL 规律和 HTML 结构,确定如何提取链接与数据。
2. 使用 requests + BeautifulSoup 逐层抓取
这是最基础也是最灵活的方式。利用 requests 发送 http 请求,用 BeautifulSoup 解析 HTML 内容。
立即学习“Python 免费学习笔记(深入)”;
示例流程:
- 请求首页,提取所有详情页的 URL 链接
- 遍历这些链接,逐个请求并解析详情页内容
- 如有需要,继续从详情页跳转到下一层
代码片段示例:
import requests from bs4 import BeautifulSoup <h1> 第一层:获取列表页中的详情链接 </h1><p>list_url = "<a href="https://www.php.cn/link/ca14cd6c279d15639a51915b4b7917bc">https://www.php.cn/link/ca14cd6c279d15639a51915b4b7917bc</a>" res = requests.get(list_url) soup = BeautifulSoup(res.text, 'html.parser')</p><p>detail_urls = [a['href'] for a in soup.select('.news-list a')]</p> <div class="aritcle_card"> <a class="aritcle_card_img" href="/ai/2358"> <img src="https://img.php.cn/upload/ai_manual/001/246/273/176127600344295.png" alt=" 面多多 "> </a> <div class="aritcle_card_info"> <a href="/ai/2358"> 面多多 </a> <p> 面试鸭推出的 AI 面试训练平台 </p> <div class=""> <img src="/static/images/card_xiazai.png" alt=" 面多多 "> <span>30</span> </div> </div> <a href="/ai/2358" class="aritcle_card_btn"> <span> 查看详情 </span> <img src="/static/images/cardxiayige-3.png" alt=" 面多多 "> </a> </div> <h1> 第二层:抓取每个详情页的内容 </h1><p>for url in detail_urls: detail_res = requests.get(url) detail_soup = BeautifulSoup(detail_res.text, 'html.parser') title = detail_soup.find('h1').text content = detail<em>soup.find('div', class</em>='content').text print(title, content)
3. 使用 Scrapy 框架高效处理多级抓取
对于复杂项目,推荐使用 Scrapy 框架,它原生支持请求链式调用,适合处理多层级跳转。
核心机制是通过 yield scrapy.Request() 将解析出的链接作为新请求加入队列,并传递 回调函数 和元数据。
示例 Spider 结构:
import scrapy <p>class MultiLevelSpider(scrapy.Spider): name = 'multilevel' start_urls = ['<a href="https://www.php.cn/link/ca14cd6c279d15639a51915b4b7917bc">https://www.php.cn/link/ca14cd6c279d15639a51915b4b7917bc</a>']</p><pre class="brush:php;toolbar:false;"><pre class="brush:php;toolbar:false;">def parse(self, response): # 提取详情页链接 for href in response.css('.news-list a::attr(href)').getall(): yield response.follow(href, self.parse_detail) def parse_detail(self, response): # 解析详情页 title = response.css('h1::text').get() content = response.css('.content::text').get() # 可在此基础上继续跳转至第三层 comment_url = response.css('.comment-link::attr(href)').get() if comment_url: yield response.follow(comment_url, self.parse_comment, meta={'title': title}) def parse_comment(self, response): # 解析评论页,同时获取之前传递的数据 title = response.meta['title'] comments = response.css('.comment p::text').getall() yield { 'title': title, 'comments': comments}
4. 注意事项与优化建议
实际抓取过程中需注意以下几点,避免被封 IP 或数据遗漏:
- 设置合理的 User-Agent 和请求间隔(time.sleep),模拟真实访问行为
- 使用 session 保持会话状态,提高效率
- 对异常链接做容错处理(try-except),防止程序中断
- 避免过度 并发,遵守 robots.txt 协议
- 考虑使用代理池应对反爬机制
基本上就这些。掌握页面跳转逻辑,结合合适的 工具,就能稳定抓取多级网页数据。关键是分步处理、层层递进,别一次性想把所有逻辑塞进一个函数里。