抓取动态网站

动态网站在初始页面加载后使用 JavaScript 加载内容。本指南介绍如何使用 FourA 的 browser endpoint 从这些网站采集数据。

问题所在

向高度依赖 JavaScript 的网站发送标准 HTTP request 时,只会获取到 HTML 外壳,而非实际内容。所需的数据(商品列表、价格、搜索结果)是在浏览器渲染页面后由 JavaScript 加载的。

在 React、Vue、Angular 和 Next.js 等现代框架中,这种情况越来越普遍。

解决方案:Browser Request

FourA 的 browser endpoint (POST /api/browser/) 会在 Chrome 浏览器实例中打开目标 URL,并执行以下操作:

  1. 加载页面
  2. 执行所有 JavaScript
  3. 等待内容渲染
  4. 返回完整渲染的 HTML

第 1 步:确认所需内容

发起 request 之前,请在浏览器中访问目标页面,并使用 DevTools (F12) 查找一段文本或元素,以确认内容已加载完成。例如:

  • JS 渲染后出现的商品名称
  • 渲染后 HTML 中的 CSS 类,例如 product-grid
  • 仅在数据加载完成后才出现的文本字符串,例如 "results"

第 2 步:发送 Browser Request

curl -X POST https://eu.api.foura.ai/api/browser/ \
  -H "X-API-Key: YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "url": "https://example.com/products",
    "timeout_ms": 15000,
    "checkText": "product-grid"
  }'

checkText 选项指示 FourA 在页面加载完成后检查是否存在 "product-grid"。如果不存在,request 将返回失败并带有 checkText:product-grid not found,失败的 request 不计费。checkText 不会让 FourA 额外等待该文本。

步骤 3:解析 HTML

response 在 body 字段中包含完全渲染的 HTML。使用您常用的库进行解析:

Python (BeautifulSoup)

import requests
from bs4 import BeautifulSoup

resp = requests.post("https://eu.api.foura.ai/api/browser/", headers={
    "X-API-Key": "YOUR_API_KEY",
    "Content-Type": "application/json"
}, json={
    "url": "https://example.com/products",
    "timeout_ms": 15000,
    "checkText": "product-grid"
})

html = resp.json()["body"]
soup = BeautifulSoup(html, "html.parser")

for product in soup.select(".product-card"):
    name = product.select_one(".product-name").text.strip()
    price = product.select_one(".product-price").text.strip()
    print(f"{name}: {price}")

Node.js (cheerio)

import * as cheerio from 'cheerio';

const resp = await fetch('https://eu.api.foura.ai/api/browser/', {
  method: 'POST',
  headers: { 'X-API-Key': 'YOUR_API_KEY', 'Content-Type': 'application/json' },
  body: JSON.stringify({
    url: 'https://example.com/products',
    timeout_ms: 15000,
    checkText: 'product-grid'
  })
});

const { body: html } = await resp.json();
const $ = cheerio.load(html);

$('.product-card').each((i, el) => {
  console.log($(el).find('.product-name').text(), $(el).find('.product-price').text());
});

故障排查

内容仍然为空?

  • 确认页面是否确实使用了 JavaScript 渲染(对比“查看源代码”与 DevTools)
  • 增加 timeout_ms:部分页面加载较慢
  • 检查页面是否需要身份验证或 cookie(使用 cookies 参数)

出现验证页面?

  • 对于单个 HTTP request,请切换到 proxy endpoint(POST /api/proxy/)以使用自动 IP 轮换。
  • 若要通过 proxy 路由浏览器 request,请传递浏览器 endpoint 的 proxy 参数。proxy endpoint 仅封装单个 HTTP request,不支持浏览器 request。

proxy 参数接收早期 response 返回的不透明 proxy ID,切勿传入您自己的 proxy 地址。请先执行一次 POST /api/proxy/(或 POST /api/auto/)调用,读取其返回的 proxy 字段(在 Auto 上为 session.proxy),然后将其固定:

# Step 1: let FourA find a working exit. The response carries "proxy": "A1B2C3".
curl -X POST "https://eu.api.foura.ai/api/proxy/" \
  -H "X-API-Key: YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"maxTries": 5, "request": {"method": "GET", "url": "https://example.com/products"}}'

# Step 2: render through that same exit.
curl -X POST "https://eu.api.foura.ai/api/browser/" \
  -H "X-API-Key: YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"url": "https://example.com/products", "proxy": "A1B2C3", "timeout_ms": 20000}'

发送地址而非 ID 会返回 400 Invalid proxy format。完整模式参见 在多个请求间复用 Proxy。

后续步骤

更新于: 2026年10月15日