跳到正文

更新

Crawlee Crawl4AI Apify 代理配置

  1. Crawlee:new ProxyConfiguration({ proxyUrls: [...] }),作为爬虫的 proxyConfiguration 传入。
  2. Crawl4AI:CrawlerRunConfig(proxy_config=ProxyConfig(server, username, password))。
  3. Apify:把代理地址填进 Actor 的 Input 页「Proxy and browser configuration」,或在代码里调用 Actor.createProxyConfiguration({ proxyUrls })。不指定会话时,Crawlee 和 Apify 会轮流使用这些地址。

买完先验一次示例输出

curl -x http://USER:PASS@HOST:PORT https://ipinfo.io/json

ip
203.0.113.24
country
US
和你下单时选的国家一致
第 15 天
203.0.113.24
租期内还是同一个地址
IP 为文档示例地址(203.0.113.0/24),不是真实出口
01 / 13一个代理地址,三个填法

三个采集工具里代理写在哪、各自怎么轮换,以及怎样让一个会话固定在同一个 IP 上。

一个代理地址,三个填法

三个工具都接受同一种代理写法:http://LOGIN:PASSWORD@HOST:PORT;Crawl4AI 也可以把主机、端口、账号、密码分开填。Crawlee 和 Apify 还能给一个列表,没有会话 ID 时轮流使用。请用代理的 HTTP 端口:Crawl4AI 和 Crawlee 里的 Playwright、Puppeteer 爬虫驱动的是基于 Chromium 的浏览器,而 Chromium 系浏览器一般不支持带账号密码的 SOCKS5。

每个示例都请求 https://ipinfo.io/json,输出里能看到出口的国家和网络。

Crawlee:ProxyConfiguration 与会话

import { HttpCrawler, ProxyConfiguration } from 'crawlee';

const proxyConfiguration = new ProxyConfiguration({
    proxyUrls: ['http://LOGIN:PASSWORD@HOST:2222'],
});

const crawler = new HttpCrawler({
    proxyConfiguration,
    useSessionPool: true,
    persistCookiesPerSession: true,
    async requestHandler({ body }) {
        console.log(body.toString()); // ipinfo.io JSON: check country and org
    },
});

await crawler.run(['https://ipinfo.io/json']);

HttpCrawler 适用于任意内容类型,CheerioCrawler 只期望 HTML。useSessionPool 和 persistCookiesPerSession 让同一会话的 Cookie 放在一起,Crawlee 检测到被拦截时会换新会话。

Crawlee:会话与分层

按 Crawlee 指南,没有会话 ID 时代理地址轮流使用,有会话 ID 时该会话复用同一个代理。tieredProxyUrls 让 Crawlee 在检测到被拦截时升到更高一层的代理,而且只有通过爬虫使用这个配置时才生效。

Crawlee 的会话对应的是一个代理地址,不是一个出口 IP。如果这个地址是轮换网关,新连接建立时仍可能换 IP;IP 本身必须不变时,要在网关一侧(开会话或用固定 IP)来定。

Crawl4AI:CrawlerRunConfig 上的 proxy_config

import asyncio
from crawl4ai import AsyncWebCrawler, BrowserConfig, CrawlerRunConfig, ProxyConfig

run_config = CrawlerRunConfig(
    proxy_config=ProxyConfig(
        server="http://HOST:2222",
        username="LOGIN",
        password="PASSWORD",
    )
)

async def main():
    async with AsyncWebCrawler(config=BrowserConfig()) as crawler:
        result = await crawler.arun(url="https://ipinfo.io/json", config=run_config)
        print(result.html)

asyncio.run(main())

Crawl4AI 文档建议按次在 CrawlerRunConfig 上设代理,BrowserConfig 的 proxy 参数已弃用。

Crawl4AI:轮换

有多个代理时,Crawl4AI 文档提供了 crawl4ai.proxy_strategy 里的 RoundRobinProxyStrategy,通过 CrawlerRunConfig 的 proxy_rotation_strategy 挂上;ProxyConfig.from_env 会读 PROXIES 变量,格式是 ip:port:user:pass,多个用逗号分隔。

用轮换网关只需要一个代理:网关在每个新连接上给新 IP。手里有好几个独立 IP 时再加轮换策略。

Apify Actor:加自己代理的两种方式

在控制台里打开 Actor 的 Input 页,滚动到 Proxy and browser configuration,填入你的代理地址。Apify 的页面说明,只有输入模式里定义了该项的 Actor 才会出现这个字段,没有这个字段的 Actor 无法在 Input 页接收你的代理。

在自己写的 Actor 代码里,用 proxyUrls 列表创建代理配置并交给爬虫。Apify 文档说明 proxyConfiguration.newUrl(sessionId) 返回下一个地址,不给会话 ID 时自定义地址轮流使用。

Apify SDK:Actor.createProxyConfiguration

import { Actor } from 'apify';
import { HttpCrawler } from 'crawlee';

await Actor.init();

const proxyConfiguration = await Actor.createProxyConfiguration({
    proxyUrls: ['http://LOGIN:PASSWORD@HOST:2222'],
});

const crawler = new HttpCrawler({
    proxyConfiguration,
    async requestHandler({ body }) {
        await Actor.pushData({ response: body.toString() });
    },
});

await crawler.run(['https://ipinfo.io/json']);
await Actor.exit();

在 Actor 内运行。替换占位符,然后查看保存的响应里的代理国家。

代理填在哪

代理填在哪
项目设置位置不带会话时的轮换
Crawlee爬虫的 proxyConfiguration 选项,ProxyConfiguration({ proxyUrls })地址轮流使用;会话 ID 固定一个地址
Crawl4AICrawlerRunConfig 上的 proxy_config默认不轮换;有列表时加 RoundRobinProxyStrategy
Apify ActorInput 页的 Proxy and browser configuration,或 Actor.createProxyConfiguration自定义地址轮流使用;newUrl(sessionId) 固定一个

依据来源里列出的 Crawlee、Crawl4AI、Apify 文档页,均于 2026 年 11 月 20 日读过。工具版本会变,请以你所用版本的页面为准。

用 Proxy302 怎么做

每段代码都只需要 Proxy302 代理的四个值:主机、端口、账号和密码。做采集用动态住宅流量,按 GB 计费(低价线路每 GB US$1.50,优质线路每 GB US$5);HTTP 在 proxy.proxy302.com 的 2222 端口,SOCKS5 在 3333 端口,同一组账号密码两种协议都能用。不开会话时每个新连接换一个 IP,保持着的连接 IP 不变。

如果对方要求固定地址,可以租机房 IP:1 天 US$0.25,30 天 US$3.99,购买时选 HTTP 或 SOCKS5。没有免费试用,最小购买单位是 1 GB 动态住宅流量,或一个 IP 租一天。

用 Proxy302 怎么做:步骤

  1. 第 1 步

    打开动态住宅页面,选择线路,点击「确认费用并生成」。要固定 IP 的话,改去机房按 IP 购买页买一个机房 IP。

  2. 第 2 步

    打开「我的资产」,把 HOST(地址)、PORT(端口)、USERNAME(账号)、PASSWORD(密码)原样复制出来。

  3. 第 3 步

    填进本文的代码里,端口要和协议对应:HTTP 用 2222,SOCKS5 用 3333。

  4. 第 4 步

    用代码请求 https://ipinfo.io/json,看 country 和 org 是不是代理的信息,而不是你自己网络的。

排查

代理返回 407 或认证错误

检查 地址里的 LOGIN、PASSWORD 是否和「我的资产」一致;密码里的特殊字符在地址中要做百分号编码。

处理 重新复制,或在工具支持时改用单独的账号、密码字段(Crawl4AI)。

停止条件 同一组账号在 curl 里也失败,就带订单号联系客服。

超时,或浏览器报 ERR_PROXY_CONNECTION_FAILED

检查 端口和协议是否对应:HTTP 用 2222;带账号的 SOCKS5 地址是否用在了基于 Chromium 的浏览器里。

处理 改成 http://HOST:2222 再运行,其余检查见连接失败指南。

停止条件 所有端口在你的网络里都超时,先换个网络测试,再考虑改代码。

Actor 的输入页没有代理字段

检查 这个 Actor 的输入模式里没有定义代理配置。

处理 用 createProxyConfiguration 运行你自己的 Actor,或请 Actor 作者加上该字段。

停止条件 不要去改不属于你的 Actor,改用自己的代码。

常见问题

Crawlee 怎么加自己的代理?

创建 new ProxyConfiguration({ proxyUrls: [...] }),作为爬虫的 proxyConfiguration 选项传入。Crawlee 会轮流使用这些地址,给了会话 ID 时则每个会话用一个地址。

Crawl4AI 怎么设带账号的代理?

用 CrawlerRunConfig(proxy_config=ProxyConfig(server=..., username=..., password=...))。BrowserConfig 的 proxy 参数已弃用。

Apify Actor 里能用自己的代理吗?

能。Actor 定义了该字段时,在控制台 Input 页的 Proxy and browser configuration 里填代理地址;在自己的代码里则用 proxyUrls 调用 Actor.createProxyConfiguration。

怎么让整个采集会话固定在同一个 IP?

Crawlee 或 Apify 的会话 ID 能固定同一个代理地址。如果这个地址是轮换网关,还要在网关一侧固定 IP:开会话,或改用固定 IP。

延伸阅读

Proxy302 在 GitHub 上公开的同主题配置指南:

参考资料

5
  1. Crawlee:Proxy management

    官方文档 · 确认日期:

    2026 年 11 月 20 日读过:带 proxyUrls 的 ProxyConfiguration、不带会话 ID 时的轮流使用、newUrl(sessionId)、tieredProxyUrls、useSessionPool 与 persistCookiesPerSession,以及爬虫的 proxyConfiguration 选项。

  2. Crawl4AI:Proxy and security

    官方文档 · 确认日期:

    2026 年 11 月 20 日读过:CrawlerRunConfig(proxy_config=ProxyConfig(server, username, password))、ProxyConfig.from_string 与 from_env、已弃用的 BrowserConfig 代理参数,以及 RoundRobinProxyStrategy 与 proxy_rotation_strategy。

  3. Apify 文档:Using your own proxies

    官方文档 · 确认日期:

    2026 年 11 月 20 日读过:在 Actor 的 Input 页「Proxy and browser configuration」里填自定义代理地址(仅限输入模式里定义了该项的 Actor),以及 SDK 里的 proxyConfiguration.newUrl(sessionId)。

  4. Apify SDK:Proxy management

    官方文档 · 确认日期:

    2026 年 11 月 20 日读过(据搜索结果中的页面摘要):用 proxyUrls 列表创建代理配置,以及不给会话 ID 时轮流使用。

  5. Proxy302 产品事实与价格表

    产品观察 · 确认日期:

    站点的产品事实表(数值已对照控制台、后端和价格表核对):产品、美元价格(1 PTC = 1 美元)、租期、协议与端口、数量上限、没有免费试用。

准备好试试了吗?

注册账号、充值一次,就能选动态住宅、静态 ISP 或数据中心 IP,几分钟配进你的浏览器。