惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

GbyAI
GbyAI
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
P
Proofpoint News Feed
L
Lohrmann on Cybersecurity
S
Secure Thoughts
Attack and Defense Labs
Attack and Defense Labs
人人都是产品经理
人人都是产品经理
Stack Overflow Blog
Stack Overflow Blog
W
WeLiveSecurity
O
OpenAI News
SecWiki News
SecWiki News
博客园 - Franky
NISL@THU
NISL@THU
Microsoft Azure Blog
Microsoft Azure Blog
T
Tor Project blog
Microsoft Security Blog
Microsoft Security Blog
aimingoo的专栏
aimingoo的专栏
Security Latest
Security Latest
H
Hacker News: Front Page
Google Online Security Blog
Google Online Security Blog
P
Privacy & Cybersecurity Law Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
D
Darknet – Hacking Tools, Hacker News & Cyber Security
月光博客
月光博客
李成银的技术随笔
Spread Privacy
Spread Privacy
F
Full Disclosure
F
Fortinet All Blogs
T
The Exploit Database - CXSecurity.com
Vercel News
Vercel News
AWS News Blog
AWS News Blog
WordPress大学
WordPress大学
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
V
Visual Studio Blog
J
Java Code Geeks
博客园 - 三生石上(FineUI控件)
G
Google Developers Blog
云风的 BLOG
云风的 BLOG
博客园 - 司徒正美
Engineering at Meta
Engineering at Meta
Last Week in AI
Last Week in AI
P
Palo Alto Networks Blog
宝玉的分享
宝玉的分享
T
True Tiger Recordings
N
News and Events Feed by Topic
酷 壳 – CoolShell
酷 壳 – CoolShell
Cisco Talos Blog
Cisco Talos Blog
N
News | PayPal Newsroom
S
SegmentFault 最新的问题
Jina AI
Jina AI

掘金

“杀!杀!杀!”、“我最讨厌事后道歉”——骂“杀哥”之前,谁还没当过情绪崩溃的人 juejin.cn juejin.cn juejin.cn juejin.cn juejin.cn juejin.cn juejin.cn juejin.cn juejin.cn juejin.cn 人人都在鼓吹的OPC,我想给你泼盆冷水 juejin.cn juejin.cn juejin.cn Redis内存用爆了,原来我们都忽略了这个配置 juejin.cn juejin.cn juejin.cn juejin.cn juejin.cn juejin.cn juejin.cn Android 专家岗 Kotlin 面试题:能答出这些,说明你对语言设计有自己的理解 juejin.cn juejin.cn 业务系统集成 OpenClaw 多 Agent 方案:从架构到落地的完整指南 juejin.cn juejin.cn juejin.cn juejin.cn juejin.cn juejin.cn juejin.cn juejin.cn juejin.cn juejin.cn juejin.cn 四、Agent 评估与可观测性:LangSmith 与客服 A/B 测试 🍃 MongoDB 从入门到上手:一篇写给新手的科普指南 juejin.cn juejin.cn juejin.cn juejin.cn RAG 系列(十九):增量更新——知识库如何保持新鲜 juejin.cn juejin.cn juejin.cn juejin.cn juejin.cn 当 00 后开始用 token 给学校送礼 juejin.cn SwiftUI 多线程与并发编程深度总结 juejin.cn juejin.cn juejin.cn Combine 架构模式:构建响应式应用的蓝图 Combine 高级实践:多线程调度、调试与测试 SSE(Server-Sent Events)完全指南 juejin.cn juejin.cn juejin.cn 阿里云峰会 Agent Native 基础设施专场邀您参加! juejin.cn juejin.cn juejin.cn juejin.cn juejin.cn juejin.cn TRAE SOLO 移动版的安装与测试 juejin.cn Vue 的 template 标签不能用 v-show?底层机制+踩坑复盘+生产级解决方案 juejin.cn juejin.cn juejin.cn juejin.cn juejin.cn juejin.cn juejin.cn juejin.cn juejin.cn juejin.cn juejin.cn juejin.cn everything-claude-code 在 Codex 的应用:不要照搬全家桶,而是做一套更聪明的增强层 juejin.cn juejin.cn juejin.cn juejin.cn juejin.cn juejin.cn 阻塞队列之SynchronousQueue juejin.cn juejin.cn 小程序成长计划正式接入Hy3 preview juejin.cn juejin.cn juejin.cn juejin.cn juejin.cn
Crawlee StagehandCrawler:自然语言点 Load More 的工程化爬虫
大鹏AI教育 · 2026-05-21 · via 掘金

作者:张大鹏 | 大鹏AI教育

标签:AI 爬虫 Crawlee Stagehand 浏览器自动化

image.png

阅读提示

列表页上的 「Load More / 加载更多」 是动态爬虫的经典噩梦:

  • 按钮 class 名随前端改版变化
  • 点击后要等 XHR,条目数不固定
  • 翻到底没有明确「最后一页」信号

传统 Playwright 脚本靠 CSS 选择器硬写,维护成本高。Crawlee 在 v3.16 起引入 StagehandCrawler:在 Crawlee 的请求队列、重试、存储体系上,叠加 Stagehand 的自然语言浏览器操作——你可以用一句话描述「点击加载更多,直到没有更多按钮」,而不是死磕 .btn-load-more-v2

这篇面向合规采集:公开列表、授权后台、压测环境。不涉及绕过验证码、登录墙或反爬。


1. StagehandCrawler 在 Crawlee 栈里的位置

Crawlee 本身解决的是工程问题:

  • RequestQueue / Dataset 持久化
  • 自动重试、会话、代理钩子
  • Playwright / Puppeteer 统一入口

Stagehand 解决的是交互表达问题:用自然语言描述「点哪个、填什么、等到什么出现」。

合在一起,典型链路是:

Seed URL → StagehandCrawler 打开页
         → act("click Load More until gone")
         → extract 列表字段入 Dataset
         → enqueue 详情页(可选)

image.png

边界:NL 操作不是免维护。页面结构大变时,指令语义可能失效,仍需监控失败率并回退到 getByRole 等显式 locator。


2. 最小示例:自然语言点 Load More

安装(版本号以项目为准):

npm i crawlee @crawlee/playwright stagehand

核心爬虫骨架:

import { StagehandCrawler } from '@crawlee/playwright';
import { Dataset } from 'crawlee';

const crawler = new StagehandCrawler({
  maxRequestsPerCrawl: 50,
  launchContext: {
    launchOptions: { headless: true },
  },
  async requestHandler({ page, request, stagehand, log }) {
    await page.goto(request.url, { waitUntil: 'domcontentloaded' });

    // 自然语言:反复点「加载更多」,直到按钮消失或不可点
    await stagehand.act(
      'If there is a "Load More" or "加载更多" button, click it and wait for new list items. Repeat until the button is gone or disabled. Do not click more than 10 times.'
    );

    const items = await stagehand.extract({
      instruction: 'Extract all article cards: title, url, summary if visible.',
      schema: {
        type: 'object',
        properties: {
          items: {
            type: 'array',
            items: {
              type: 'object',
              properties: {
                title: { type: 'string' },
                url: { type: 'string' },
                summary: { type: 'string' },
              },
              required: ['title', 'url'],
            },
          },
        },
        required: ['items'],
      },
    });

    await Dataset.pushData(items.items);
    log.info(`Saved ${items.items.length} items from ${request.url}`);
  },
});

await crawler.run(['https://example.com/articles']);

要点:

  1. act 要写清停止条件(最多点 10 次),防止无限循环
  2. extract 必须绑 JSON Schema,避免幻觉字段
  3. Crawlee 的 maxRequestsPerCrawl 控制规模,配合限速

3. 工程化:队列、重试与观测

Load More 场景建议再加:

import { Configuration } from 'crawlee';

Configuration.getGlobalConfig().getEventManager().on('migrating', () => {
  // 进程迁移时的钩子
});

const crawler = new StagehandCrawler({
  maxRequestRetries: 2,
  requestHandlerTimeoutSecs: 120,
  minConcurrency: 1,
  maxConcurrency: 2, // 公开站别拉太高
  failedRequestHandler({ request, error }) {
    console.error(`Failed ${request.url}:`, error);
    // 留截图、HTML 片段供人工复盘
  },
  // ... requestHandler 同上
});

和纯 Playwright 脚本对比

手写 PlaywrightStagehandCrawler
Load More 交互手写 loop + selectorNL act + 停止条件
持久化自建Dataset / Queue 内置
失败重试自建Crawlee 内置
合规限速需自写配合 concurrency + sleep
可预测性高(selector 明确)中(需 Schema + 监控)

image.png


4. 合规边界

Stagehand 的「自然语言点按钮」不等于可以点「绕过验证」「同意所有 Cookie 后强行登录」这类违规操作。

允许禁止
公开列表 Load More未授权登录后采数据
遵守 robots.txt破解验证码 / 滑块
低速、低并发分布式压测式抓取
结构化公开字段采用户私信、手机号等

遇到以下情况应停止任务并告警,而非让 Stagehand「想办法」:

  • HTTP 403 / 429 持续出现
  • 出现 CAPTCHA / 人机验证
  • Terms 明确禁止 automated access

5. 上线检查清单

image.png

  • act 是否写清 最大点击次数 与停止条件
  • extract 是否绑定了 JSON Schema
  • Crawlee maxConcurrency 是否控制在合理范围
  • 是否有 failedRequestHandler 留证
  • 目标站是否公开或已授权
  • 是否尊重 robots.txt
  • 遇验证码是否停止而非绕过
  • 首批数据是否人工对照页面抽检

6. 结论

Crawlee StagehandCrawler 适合 「列表 + Load More + 字段抽取」 这类交互重复、选择器易变的合规场景。推荐组合:

Crawlee 管队列与重试,Stagehand 管 NL 交互,Schema 管输出,人工管抽检与合规。

别把它当成反爬神器;把它当成降低 selector 维护成本的工程组件,价值才可持续。


参考来源

  1. Crawlee Documentation
  2. Stagehand
  3. Playwright Best Practices

作者:张大鹏|来源:大鹏AI教育
标签:AI · 爬虫 · Crawlee · Stagehand
原创内容,转载需授权