# 爬虫特工工具箱 (Scrapling Tools) 你擅长撰写 Python 脚本。 你在自己的目录下有权限编写任意的 `xxx.py`,你也可以执行它们获取数据。 ## 使用 Scrapling (特工必修课) ```python # 编写抓取脚本的基本框架 import os import json from scrapling import StealthyFetcher # 务必优先检测是否存在被拦截要求的代理配置 proxy = os.getenv('PROXY_URL') # 如果确实因为频繁抓取被封,可以抛错然后让自己的 js 容器去触发 human request fetcher = StealthyFetcher(proxy=proxy) if proxy else StealthyFetcher() page = fetcher.get("https://你想抓取的URL.com") # 你可以使用 Scrapling 强大的 CSS/XPath 选择器找到结果 items = page.css(".article-title::text") results = [item for item in items] # 把抓取的精华以 json 返回 with open('workspace/data/output/result.json', 'w') as f: json.dump(results, f, ensure_ascii=False) ``` ## 异常熔断器 (求助护盾机制) 你可以使用 `execSync` 来调用 `lemon.js` 向顶部发送救救孩子的指令: ```javascript const { execSync } = require('child_process'); try { // 如果你在用 child_process 运行刚才写的 fetcher.py 时,stderr 报了 Access Denied execSync(`python3 fetcher.py`); } catch (e) { if (e.stderr.toString().includes('AccessDenied') || e.stderr.toString().includes('Captcha')) { // 启动人类救援系统 execSync(`node bin/lemon.js human request --from "${myAgentId}" --type api_key --key "PROXY_URL" --reason "我被 Cloudflare 的高级验证码拦截了,Scrapling的默认绕过失败,请给我配置收费动态代理URL或给对应的第三方接口"`); process.exit(1); // 安全下线,等老板给配置后重启 } } ```