首先明确页面层级结构,再通过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 flexRow">
<div class="artcardd flexRow">
<a class="aritcle_card_img" href="/ai/890" title="MaxAI"><img
src="https://img.php.cn/upload/ai_manual/000/000/000/175679988136887.png" alt="MaxAI" onerror="this.onerror='';this.src='/static/lhimages/moren/morentu.png'" ></a>
<div class="aritcle_card_info flexColumn">
<a href="/ai/890" title="MaxAI">MaxAI</a>
<p>MaxAI.me是一款功能强大的浏览器AI插件,集成了多种AI模型。</p>
</div>
<a href="/ai/890" title="MaxAI" class="aritcle_card_btn flexRow flexcenter"><b></b><span>下载</span> </a>
</div>
</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;">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协议
- 考虑使用代理池应对反爬机制
基本上就这些。掌握页面跳转逻辑,结合合适的工具,就能稳定抓取多级网页数据。关键是分步处理、层层递进,别一次性想把所有逻辑塞进一个函数里。









