Scrapy · 代理配置

Scrapy 代理配置

Python crawling framework with its own downloader middleware stack. 按下面的配置把它指向 Eveses 住宅池 — 地区写在用户名里,所以换国家不需要动其他任何设置。

proxy.eveses.com:12321 · SOCKS5 在 32325 · 199 个国家 · $7.35/GB 起

注意

Scrapy will not pick the proxy up from `http_proxy` for HTTPS targets. It has to go through `HttpProxyMiddleware`, and the credentials must be set on the request rather than in the URL — a proxy URL with a password in it is silently stripped from `meta["proxy"]`.

配置

# settings.py
DOWNLOADER_MIDDLEWARES = {
    "scrapy.downloadermiddlewares.httpproxy.HttpProxyMiddleware": 110,
    "myproject.middlewares.EvesesProxyMiddleware": 100,   # must run BEFORE 110
}

# middlewares.py
import base64

class EvesesProxyMiddleware:
    USER = "eveses_user_country-us"     # add geo tokens to the username
    PASS = "YOUR_PROXY_PASSWORD"

    def process_request(self, request, spider):
        request.meta["proxy"] = "http://proxy.eveses.com:12321"
        creds = base64.b64encode(f"{self.USER}:{self.PASS}".encode()).decode()
        request.headers["Proxy-Authorization"] = f"Basic {creds}"

Scrapy 相关说明

Give the middleware a lower number than HttpProxyMiddleware (110) so it sets `meta["proxy"]` first.

Rotate per request by appending a fresh `_session-…` token to the username inside `process_request`.

Leave `RETRY_TIMES` at its default; a blocked exit IP retries onto a different one automatically.

换国家而不动 Scrapy

德国eveses_user_country-de
固定 30 分钟eveses_user_country-de_session-ab12cd34_lifetime-30m

地区和会话都写在用户名里,而不是主机或端口。换国家时本页所有配置保持原样 — 你只改一个字符串。可定位 199 个国家,其中大多数还支持城市与州/省令牌。

常见问题

如何在 Scrapy 里设置代理?›
把它指向 proxy.eveses.com,HTTP 用端口 12321,SOCKS5 用 32325,并用你的 Eveses 代理用户名和密码认证。确切写法见上面的代码片段。 Scrapy will not pick the proxy up from `http_proxy` for HTTPS targets. It has to go through `HttpProxyMiddleware`, and the credentials must be set on the request rather than in the URL — a proxy URL with a password in it is silently stripped from `meta["proxy"]`.
如何选择 Scrapy 的出口国家?›
在用户名后追加地区令牌,而不是改主机:eveses_user_country-de 从德国出口,eveses_user_country-us 从美国出口。这样可定位 199 个国家,城市或州令牌还能进一步收窄。Scrapy 的其他配置一概不变。
如何在 Scrapy 中让多次请求保持同一个 IP?›
加一个会话令牌 — eveses_user_country-us_session-ab12cd34_lifetime-30m — 带该用户名的每个请求都会从同一地址出去,直到生存期结束。去掉令牌,每个请求就会轮换到新的 IP。
这要多少钱?›
住宅流量按 GB 从你的 Eveses 钱包扣费:1 GB 时 $7.35/GB,大流量降至 $2.57/GB。没有按工具收费,同一套凭据可同时用于所有工具。

其他集成

用住宅 IP 跑 Scrapy

1 GB 时 $7.35/GB,大流量 $2.57/GB — 所有工具共用同一套凭据。

获取代理 →