# 用 pip 安装 firecrawl-py
from firecrawl import Firecrawl
firecrawl = Firecrawl(api_key="YOUR_API_KEY")
# 先用 basic 代理尝试
try:
content = firecrawl.scrape("https://example.com")
# 检查是否返回了错误状态码
status_code = content.get("metadata", {}).get("statusCode")
if status_code in [401, 403, 500]:
print(f"收到状态码 {status_code},改用 stealth 代理重试")
# 使用 stealth 代理重试
content = firecrawl.scrape("https://example.com", proxy="stealth")
print(content["markdown"])
except Exception as e:
print(f"错误:{e}")
# 出现异常时改用 stealth 代理重试
try:
content = firecrawl.scrape("https://example.com", proxy="stealth")
print(content["markdown"])
except Exception as e:
print(f"stealth 代理仍然失败:{e}")