前言:爬虫的终极武器
凌晨2点44分,我看着又一个被Cloudflare拦截的请求,陷入了沉思。普通的browser工具已经不够用了,Google、Amazon、LinkedIn这些网站比你想象的更聪明。
直到我发现了camofox——一个专门绕过bot检测的反检测浏览器。世界突然变得美好了。
为什么需要camofox?
| 场景 | 普通浏览器 | camofox |
|---|---|---|
| Google搜索 | 被验证码拦截 | 正常使用 |
| Amazon数据 | 频繁被封 | 稳定抓取 |
| LinkedIn档案 | 登录即封号 | 需要cookies辅助 |
| 普通网站 | OK | OK(但不必要) |
官方建议:优先使用camofox工具而不是Chrome/内置浏览器。只有在需要登录cookies时才考虑user profile。
camofox API 详解
camofox_create_tab
创建新标签页
camofox_snapshot
获取页面快照
camofox_click
点击元素
camofox_type
输入文本
camofox_navigate
页面导航
camofox_scroll
滚动页面
camofox_screenshot
页面截图
camofox_evaluate
执行JavaScript
camofox_close_tab
关闭标签页
实战一:Google搜索
// 1. 创建标签页
const tab = await camofox_create_tab({
url: "about:blank"
});
// 2. 使用搜索宏导航
await camofox_navigate({
tabId: tab.tabId,
macro: "@google_search",
query: "OpenClaw tutorials 2026"
});
// 3. 获取页面快照
const snap = await camofox_snapshot({ tabId: tab.tabId });
// 4. 提取搜索结果
const results = await camofox_evaluate({
tabId: tab.tabId,
expression: `
Array.from(document.querySelectorAll('.g a'))
.slice(0, 10)
.map(a => ({ title: a.textContent, href: a.href }))
`
});
// 5. 关闭标签页
await camofox_close_tab({ tabId: tab.tabId });
实战二:数据抓取
// 抓取产品页面
const tab = await camofox_create_tab({
url: "https://example.com/products"
});
// 获取快照(带ref引用)
const snap = await camofox_snapshot({
tabId: tab.tabId,
refs: "aria" // 使用aria引用,跨调用稳定
});
// 点击分页按钮
await camofox_click({ tabId: tab.tabId, ref: "e5" });
// 提取数据
const data = await camofox_evaluate({
tabId: tab.tabId,
expression: `
JSON.stringify(
Array.from(document.querySelectorAll('.product'))
.map(p => ({
name: p.querySelector('h3')?.textContent,
price: p.querySelector('.price')?.textContent,
rating: p.querySelector('.rating')?.dataset.score
}))
)
`
});
实战三:表单填写
const tab = await camofox_create_tab({
url: "https://example.com/contact"
});
const snap = await camofox_snapshot({ tabId: tab.tabId });
// 填写输入框
await camofox_type({
tabId: tab.tabId,
ref: "e2", // name输入框的ref
text: "诗中"
});
await camofox_type({
tabId: tab.tabId,
ref: "e3", // email输入框的ref
text: "hello@miaoquai.com"
});
// 点击提交
await camofox_click({
tabId: tab.tabId,
ref: "e6" // submit按钮的ref
});
高级技巧:Cookies导入
对于需要登录的网站,可以先导入cookies:
// 导入Netscape格式的cookies
await camofox_import_cookies({
cookiesPath: "/path/to/cookies.txt",
domainSuffix: ".linkedin.com" // 只导入特定域
});
// 然后正常访问需要登录的页面
const tab = await camofox_create_tab({
url: "https://linkedin.com/feed"
});
安全提醒:不要将cookies文件提交到版本控制系统。使用环境变量或安全存储管理敏感信息。
搜索宏列表
camofox内置了多个搜索宏,一键搜索热门平台:
| 宏 | 平台 | 示例 |
|---|---|---|
@google_search |
通用搜索 | |
@youtube_search |
YouTube | 视频搜索 |
@amazon_search |
Amazon | 商品搜索 |
@reddit_search |
帖子搜索 | |
@linkedin_search |
人脉搜索 | |
@twitter_search |
X/Twitter | 推文搜索 |
Snapshot Refs 说明
- role(默认):基于角色和名称的引用,如e1, e2
- aria:Playwright aria-ref id,跨调用更稳定
建议:当需要在多次snapshot之间保持元素引用时,使用
refs="aria"。对于单次操作,默认的role引用足够了。
最佳实践
- 优先使用camofox而非内置浏览器
- 使用snapshot获取refs,不要硬编码CSS选择器
- 操作完成后关闭标签页,释放资源
- 对于分页内容,使用scroll+snapshot组合处理
- JavaScript提取数据时用JSON.stringify返回结构化数据
- 频繁使用screenshot检查页面状态