技能创建器
skill-creator
创建、改进和评估自定义技能,把专业流程转化为可重复使用的 AI 能力。
rotate-scraping-proxies【数据与研究】Escalate blocked scraping campaigns with provider-neutral proxy rotation — decide between datacenter, residential, and mobile pools, integrate rotation with scrapling, configure session stickiness for stateful flows, monitor cost and health, and stay inside legal and ethical boundaries. Use as the next step after `headless-web-scraping` client-side s…
独立 GitHub 快照 · 自动分类 · 以原作者文档为准
headless-web-scraping client-side stealth (StealthyFetcher,
rate limiting, robots.txt) is insufficient and traffic is legitimate.
license: MIT
allowed-tools: Bash Read Write Grep Glob
metadata:
author: Philipp Thoss
version: "1.0"
domain: web-scraping
complexity: intermediate
language: Python
tags: web-scraping, proxies, rotation, residential, scrapling, networking擷阻時代之輪。末計也,貴、難、易誤用。教知不用之時亦如善用。
headless-web-scraping(Fetcher → StealthyFetcher → DynamicFetcher)已試而仍 403/429/地阻→用robots.txt 許→用勿用:公 API 存(用之)、ToS 禁自動、越地授、欺詐、堆證、鞋機、盜版。
文錄之法倫察為閘。略此為害源。
Inputs to confirm before writing any code:
得:諸問皆有可辯之答。首「否」/「未知」即停。
敗:
各類費、可察、倫殊。擇可解阻之最廉。
| 池類 | 可察 | 費 | 宜 |
|---|---|---|---|
| 中心 | 高 | $ | 無真反機之站、僅地遷 |
| 宅 | 低 | $$ | 阻中心 ASN 之站 |
| 行 | 極低 | $$ | 阻宅之站(罕) |
宅、行倫慮:經真消費連線。供商徵同模殊—有付用者,有「免 VPN」EULA 暗包者。宜選審計、入式同意。汝若不願外人擷流經汝家,勿經他人。
得:文錄擇—最廉可行、附拒高之故。
敗:
接代於 scrapling 擷。讀環境憑—勿硬碼,勿提交 .env。
import os
import random
from scrapling import Fetcher, StealthyFetcher
# Pattern A: provider-managed rotating endpoint (one URL, provider rotates per request)
PROXY_URL = os.environ["SCRAPING_PROXY_URL"] # http://user:pass@gateway.example:7777
fetcher = StealthyFetcher()
fetcher.configure(
headless=True,
timeout=60,
network_idle=True,
proxy=PROXY_URL,
)
# Pattern B: explicit pool, rotate yourself
POOL = os.environ["SCRAPING_PROXY_POOL"].split(",") # comma-separated URLs
def fetch_with_rotation(url):
proxy = random.choice(POOL)
fetcher = StealthyFetcher()
fetcher.configure(headless=True, timeout=60, proxy=proxy)
return fetcher.get(url)
得:請求成、出口 IP 變。實擷前以 IP 回顯端(如 https://api.ipify.org)驗。
敗:
-rotating 或請求標按工選輪粒,繼保池健。
# Sticky session for stateful flows (login, multi-page checkout-like crawls)
# Most providers expose a session ID via the username:
# user-session-abc123:pass@gateway.example:7777
# All requests with the same session ID exit through the same IP for ~10 min.
# Per-request rotation for anonymous bulk scraping (default)
# Pool health check — call before bulk run
def check_pool(pool, sample_size=5):
sample = random.sample(pool, min(sample_size, len(pool)))
alive = []
for proxy in sample:
try:
r = StealthyFetcher().configure(proxy=proxy, timeout=10).get(
"https://api.ipify.org"
)
if r.status == 200:
alive.append(proxy)
except Exception:
pass
return alive
# Backoff on transient proxy failures
def fetch_with_backoff(url, max_attempts=3):
for attempt in range(max_attempts):
try:
r = fetch_with_rotation(url)
if r.status not in (407, 502, 503):
return r
except Exception:
pass
time.sleep(2 ** attempt)
return None
得:態流保 cookie;匿擷示 IP 變;死代跳過。
敗:
代流按 GB 與請求計費。失控擷生失控賬。必設限與停。
import time
class ScrapeBudget:
def __init__(self, max_requests, max_duration_seconds, max_failures):
self.max_requests = max_requests
self.max_duration = max_duration_seconds
self.max_failures = max_failures
self.requests = 0
self.failures = 0
self.start = time.monotonic()
def allow(self):
if self.requests >= self.max_requests:
return False, "request cap reached"
if time.monotonic() - self.start >= self.max_duration:
return False, "time cap reached"
if self.failures >= self.max_failures:
return False, "failure cap reached (circuit breaker)"
return True, None
def record(self, success):
self.requests += 1
if not success:
self.failures += 1
budget = ScrapeBudget(max_requests=1000, max_duration_seconds=3600, max_failures=20)
for url in target_urls:
ok, reason = budget.allow()
if not ok:
print(f"Aborting: {reason}")
break
response = fetch_with_backoff(url)
budget.record(success=response is not None)
time.sleep(1) # rate limiting still applies even with rotation
得:限觸先於失控費。日誌示代功率以辨除。
敗:
gateway.、proxy=、供主機).env 入 .gitignorerobots.txt 仍守—輪不蓋之StealthyFetcher 與限速;輪貴而倫robots.txt」:輪不予許。指即指