ZH
開始使用

整合 · 自動化與擷取

搭配 Scrapling 使用 HProxy。

為每個 Scrapling fetcher、工作階段或 spider 提供 HProxy 住宅出口。
開始使用閱讀完整指南
  • 5 個步驟
  • 可用的程式碼
  • HTTP 與 SOCKS5
  • 登入資訊放在代理網址中

為什麼需要代理

為什麼要搭配 Scrapling 使用代理

Scrapling 是一個 Python 擷取框架,一個套件中有兩個 fetcher:帶有瀏覽器 TLS 指紋的純 HTTP,以及能解決 Cloudflare 驗證挑戰的隱匿瀏覽器。從 0.4 版起,它也內建了自己的 spider 框架。0.5 版移除了較舊的 Playwright fetcher DynamicFetcher,所以為 0.4 撰寫、匯入它的程式碼必須改用 StealthyFetcher。

Scrapling 會改變請求的樣貌,而它的來源位址仍然是你的。網站依位址計算請求數,所以無論指紋多好,從單一 IP 進行的爬取都會在幾百個頁面後停止。輪換式住宅 IP 池能讓每個請求或每個工作階段都有自己的位址。

Setup

在 Scrapling 中設定 HProxy

  1. 1

    安裝 fetcher:pip install "scrapling[fetchers]",然後為瀏覽器執行一次 scrapling install。

  2. 2

    從 HProxy 控制台複製你的代理主機、連接埠與登入資訊。

  3. 3

    Pass proxy= to a fetcher or a session. The HTTP fetcher also takes proxy_auth=(user, pass), which keeps the password out of the logs.

  4. 4

    For several lines, build a ProxyRotator from the list and pass it as proxy_rotator= to the session.

  5. 5

    經由工作階段擷取一次 IP 回顯頁面,在正式執行前確認位址。

值得知道

瀏覽器 fetcher 只接受 http、https、socks4 與 socks5,而帶有登入資訊的 SOCKS5 會在瀏覽器中失敗。在那裡請使用同一個代理的 HTTP 設定。

這裡顯示的主機、連接埠與登入資訊只是格式;你確切的值就在你的控制台上,可以直接複製。

Scrapling,搭配 HProxy

from scrapling.fetchers import Fetcher, StealthyFetcher

# HTTP fetcher: the login stays out of the proxy URL
page = Fetcher.get(
    "https://ipinfo.io/json",
    proxy="http://example.hproxy.com:8080",
    proxy_auth=("user-country-us", "pass"),
)
print(page.json())

# Stealth browser: same proxy, as a URL with the login inside
page = StealthyFetcher.fetch(
    "https://ipinfo.io/json",
    proxy="http://user-country-us:[email protected]:8080",
    block_webrtc=True,
)

Scrapling 個問題

常見問題

如何在 Scrapling 中輪換代理?

With a rotating gateway you do not need to: one unchanged endpoint hands out a different exit per request. For a list of fixed lines, create ProxyRotator([...]) from scrapling.fetchers and pass it as proxy_rotator= to a session. It cycles in order and accepts a custom strategy function. A session takes proxy_rotator= or proxy=, never both.

為什麼我的 Scrapling 工作階段會忽略代理?

From version 0.4 to 0.4.8, FetcherSession(proxy=...) was silently ignored and requests left from the real IP. The fix shipped in 0.4.9. Update Scrapling, then fetch an IP echo page through the session to confirm the address it shows.

為什麼我的代理密碼在 Scrapling 的瀏覽器 fetcher 中會失敗?

在 0.5.1 版之前,瀏覽器 fetcher 會把代理網址中的登入資訊以仍經百分比編碼的形式交給瀏覽器。寫成 p%40ss 的密碼會以 p%40ss 送達代理,代理就會回應 407。0.5.1 版會將它解碼。在較舊的版本上,請以包含 server、username 與 password 的字典傳入代理,它會照輸入的樣子接收登入資訊。

Scrapling 支援 SOCKS5 代理嗎?

HTTP fetcher 支援。瀏覽器 fetcher 只接受不含登入資訊的 socks5:// 設定,因為底層的瀏覽器拒絕 SOCKS5 驗證,而且會以 Invalid proxy string 拒絕 socks5h://。有使用者名稱與密碼時,請使用同一個代理的 HTTP 設定。

登入流程應該由哪個 fetcher 承載代理?

工作階段,搭配固定設定。StealthySession 會在多個請求之間保持同一個瀏覽器開啟,而登入只有在位址於整個期間都保持不變時才能維持。輪換式設定則適合用 Fetcher 逐頁讀取,不會保留任何狀態。

如何讓 Scrapling 的瀏覽器 fetcher 少用住宅流量?

Turn on disable_resources=True and block_ads=True. A browser loads images, fonts and trackers that a scraper never reads, and residential traffic is billed per gigabyte. Both options are off by default.

隨時為你準備好。

你的控制台只要十秒就到。不用業務電話,免訂閱,沒有最低金額。