Trường Hợp Sử Dụng

Xoay vòng proxy để quét CAPTCHA

Xoay proxy giúp giảm tần suất CAPTCHA bằng cách phân phối yêu cầu trên nhiều IP. Kết hợp với CaptchaAI để giải các CAPTCHA vẫn xuất hiện, bạn sẽ có được một quy trình thu thập dữ liệu đáng tin cậy có thể xử lý mọi hệ thống chống bot.

Tại sao xoay vòng proxy làm giảm CAPTCHA

Các trang web kích hoạt CAPTCHA dựa trên mẫu yêu cầu trên mỗi IP:

Yếu tố IP đơn Proxy luân phiên
Yêu cầu mỗi phút Hơn 10 trình kích hoạt CAPTCHA Phân phối trên các IP
Danh tiếng IP Suy thoái theo thời gian IP mới từ nhóm
Mẫu phiên Có thể nhìn thấy các mẫu đáng ngờ Các mô hình trải rộng trên các IP
Tính nhất quán về mặt địa lý Vị trí duy nhất Đa dạng về địa lý tự nhiên

Các loại proxy để Scraping

loại Tốt nhất cho Tỷ lệ CAPTCHA Chi phí
Khu dân cư Mục tiêu có giá trị cao (Google, Amazon) Thấp nhất $$$
Điện thoại di động Phát hiện cực thấp Thấp nhất $$$$
ISP/Static Phiên duy trì Thấp $$
Trung tâm dữ liệu Các trang web dung lượng lớn, nhẹ nhàng Cao hơn $

Khuyến nghị: Sử dụng đa dạng nguồn yêu cầu cho các trang web có trình kích hoạt CAPTCHA mạnh mẽ. Proxy trung tâm dữ liệu hoạt động cho các trang web ít được bảo vệ hơn.

Xoay vòng proxy cơ bản (Python)

import requests
import random
import time

PROXIES = [
    "http://user:pass@proxy1.example.com:8080",
    "http://user:pass@proxy2.example.com:8080",
    "http://user:pass@proxy3.example.com:8080",
]

API_KEY = "YOUR_API_KEY"

def get_random_proxy():
    proxy = random.choice(PROXIES)
    return {"http": proxy, "https": proxy}

def scrape_with_rotation(url):
    proxy = get_random_proxy()
    session = requests.Session()
    session.proxies = proxy
    session.headers.update({
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36"
    })

    resp = session.get(url)

    # If CAPTCHA appears, solve it
    if "g-recaptcha" in resp.text or "captcha" in resp.text.lower():
        from bs4 import BeautifulSoup
        soup = BeautifulSoup(resp.text, "html.parser")
        rc = soup.find("div", class_="g-recaptcha")
        if rc:
            site_key = rc["data-sitekey"]
            token = solve_captcha(site_key, url)
            resp = session.post(url, data={"g-recaptcha-response": token})

    return resp.text

def solve_captcha(site_key, page_url):
    resp = requests.get("https://ocr.captchaai.com/in.php", params={
        "key": API_KEY, "method": "userrecaptcha",
        "googlekey": site_key, "pageurl": page_url
    })
    task_id = resp.text.split("|")[1]

    for _ in range(60):
        time.sleep(5)
        result = requests.get("https://ocr.captchaai.com/res.php", params={
            "key": API_KEY, "action": "get", "id": task_id
        })
        if result.text == "CAPCHA_NOT_READY": continue
        if result.text.startswith("OK|"): return result.text.split("|")[1]
        raise Exception(result.text)
    raise TimeoutError()

Xoay proxy thông minh

Theo dõi proxy nào kích hoạt CAPTCHA và tránh chúng:

from collections import defaultdict
import random

class SmartProxyRotator:
    def __init__(self, proxies):
        self.proxies = proxies
        self.captcha_count = defaultdict(int)
        self.success_count = defaultdict(int)

    def get_proxy(self):
        # Prefer proxies with lower CAPTCHA rates
        scored = []
        for proxy in self.proxies:
            total = self.captcha_count[proxy] + self.success_count[proxy]
            if total == 0:
                score = 0.5  # Unknown proxy, neutral score
            else:
                score = self.success_count[proxy] / total
            scored.append((proxy, score))

        # Weight selection by score
        scored.sort(key=lambda x: x[1], reverse=True)
        top_proxies = scored[:max(len(scored) // 2, 1)]
        proxy = random.choice(top_proxies)[0]
        return proxy

    def report_success(self, proxy):
        self.success_count[proxy] += 1

    def report_captcha(self, proxy):
        self.captcha_count[proxy] += 1

# Usage
rotator = SmartProxyRotator(PROXIES)

def scrape(url):
    proxy = rotator.get_proxy()
    resp = requests.get(url, proxies={"http": proxy, "https": proxy})

    if "captcha" in resp.text.lower():
        rotator.report_captcha(proxy)
        # Solve CAPTCHA...
    else:
        rotator.report_success(proxy)

    return resp.text

Xoay vòng proxy với Selenium

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

def create_driver_with_proxy(proxy_url):
    options = Options()
    options.add_argument(f"--proxy-server={proxy_url}")
    options.add_argument("")
    return webdriver.Chrome(options=options)

# Rotate proxy per session
proxy = random.choice(PROXIES)
driver = create_driver_with_proxy(proxy)
driver.get("https://example.com")

Proxy + CAPTCHA Giải quyết cho Cloudflare

Việc giải Cloudflare Challenge yêu cầu chuyển proxy tới CaptchaAI:

proxy = "http://user:pass@proxy.example.com:8080"

resp = requests.get("https://ocr.captchaai.com/in.php", params={
    "key": API_KEY,
    "method": "cloudflare_challenge",
    "pageurl": "https://example.com",
    "proxy": proxy,
    "proxytype": "HTTP"
})
task_id = resp.text.split("|")[1]

# Poll for <staging-session-cookie> cookie
# Use the same proxy for subsequent requests

Thực tiễn tốt nhất

  1. Khớp địa lý proxy với mục tiêu — Sử dụng proxy của Hoa Kỳ cho các trang web của Hoa Kỳ
  2. Một phiên cho mỗi proxy — Không sử dụng lại các phiên trên các proxy khác nhau
  3. Giới hạn tỷ lệ trên mỗi proxy - Tối đa 5-10 yêu cầu/minute trên mỗi IP
  4. Theo dõi tỷ lệ CAPTCHA - Theo dõi proxy nào kích hoạt nhiều CAPTCHA hơn
  5. Sử dụng phiên cố định — Giữ cùng một proxy cho quy trình làm việc nhiều bước
  6. Xử lý lỗi proxy — Thử lại bằng proxy khác khi có lỗi kết nối

Khắc phục sự cố

vấn đề sửa chữa
Tất cả các proxy đều kích hoạt CAPTCHA Chuyển sang đa dạng nguồn yêu cầu; giảm tỷ lệ
Lỗi hết thời gian chờ proxy Loại bỏ proxy chậm khỏi nhóm; tăng thời gian chờ
Nội dung khác nhau trên mỗi proxy Một số trang web phục vụ nội dung theo địa lý cụ thể; bình thường hóa
Mã thông báo CAPTCHA không hoạt động với proxy Đảm bảo mã thông báo được sử dụng từ cùng một phiên/IP

Câu hỏi thường gặp

Tôi có cần proxy nếu tôi sử dụng CaptchaAI không?

Không hoàn toàn – CaptchaAI có thể giải CAPTCHA bất kể điều gì. Nhưng proxy làm giảm tần suất CAPTCHA xuất hiện, tiết kiệm thời gian và chi phí API.

Tôi có nên sử dụng cùng một proxy để giải và tìm kiếm CAPTCHA không?

Đối với hầu hết các loại CAPTCHA, mã thông báo hợp lệ bất kể IP. Đối với Cloudflare Challenge, bạn phải sử dụng cùng một proxy vì cookie <staging-session-cookie> bị giới hạn IP.

Tôi cần bao nhiêu proxy?

Đối với mức độ quét vừa phải (1.000 trang/day), 10-20 đa dạng nguồn yêu cầu luân phiên là đủ. Để có khối lượng lớn, hãy sử dụng nhà cung cấp proxy có tính năng xoay vòng tự động.

Hướng dẫn liên quan

Os comentários estão desativados para este artigo.