Tích Hợp

aiohttp + CaptchaAI: Giải quyết CAPTCHA không đồng bộ

aiohttp cho phép các yêu cầu HTTP không chặn trong Python. Kết hợp nó với CaptchaAI để giải quyết nhiều CAPTCHA cùng lúc mà không chặn vòng lặp sự kiện của bạn.

Yêu cầu

Yêu cầu Chi tiết
Python 3,8+
aiohttp 3,8+
Khóa API CaptchaAI Nhận một cái ở đây
pip install aiohttp

Máy khách CaptchaAI không đồng bộ

import aiohttp
import asyncio

class AsyncCaptchaAI:
    def __init__(self, api_key):
        self.api_key = api_key
        self.base_url = "https://ocr.captchaai.com"

    async def submit(self, session, params):
        """Submit a CAPTCHA task and return the task ID."""
        params["key"] = self.api_key
        async with session.get(
            f"{self.base_url}/in.php", params=params
        ) as resp:
            text = await resp.text()

        if not text.startswith("OK|"):
            raise Exception(f"Submit failed: {text}")

        return text.split("|")[1]

    async def poll(self, session, task_id, timeout=300):
        """Poll for the result with a timeout."""
        params = {
            "key": self.api_key,
            "action": "get",
            "id": task_id,
        }
        deadline = asyncio.get_event_loop().time() + timeout

        while asyncio.get_event_loop().time() < deadline:
            await asyncio.sleep(5)

            async with session.get(
                f"{self.base_url}/res.php", params=params
            ) as resp:
                text = await resp.text()

            if text == "CAPCHA_NOT_READY":
                continue
            if text.startswith("OK|"):
                return text.split("|", 1)[1]
            raise Exception(f"Solve failed: {text}")

        raise TimeoutError(f"Task {task_id} timed out after {timeout}s")

    async def solve(self, session, params, timeout=300):
        """Submit and poll in one call."""
        task_id = await self.submit(session, params)
        return await self.poll(session, task_id, timeout)

    async def get_balance(self, session):
        """Check account balance."""
        params = {"key": self.api_key, "action": "getbalance"}
        async with session.get(
            f"{self.base_url}/res.php", params=params
        ) as resp:
            return float(await resp.text())

Giải một CAPTCHA duy nhất

import asyncio
import os

async def main():
    solver = AsyncCaptchaAI(os.environ["CAPTCHAAI_API_KEY"])

    async with aiohttp.ClientSession() as session:
        # Check balance
        balance = await solver.get_balance(session)
        print(f"Balance: ${balance:.2f}")

        # Solve reCAPTCHA v2
        token = await solver.solve(session, {
            "method": "userrecaptcha",
            "googlekey": "6Le-wvkS...",
            "pageurl": "https://example.com",
        })
        print(f"Token: {token[:50]}...")

asyncio.run(main())

Giải quyết đồng thời nhiều CAPTCHA

async def solve_batch(urls, site_key):
    solver = AsyncCaptchaAI(os.environ["CAPTCHAAI_API_KEY"])

    async with aiohttp.ClientSession() as session:
        tasks = [
            solver.solve(session, {
                "method": "userrecaptcha",
                "googlekey": site_key,
                "pageurl": url,
            })
            for url in urls
        ]

        results = await asyncio.gather(*tasks, return_exceptions=True)

        for url, result in zip(urls, results):
            if isinstance(result, Exception):
                print(f"FAILED {url}: {result}")
            else:
                print(f"SOLVED {url}: {len(result)} chars")

        return results

urls = [
    "https://example.com/page1",
    "https://example.com/page2",
    "https://example.com/page3",
    "https://example.com/page4",
    "https://example.com/page5",
]
asyncio.run(solve_batch(urls, "6Le-wvkS..."))

Quét bằng cách xử lý CAPTCHA

async def scrape_with_captcha(url, site_key):
    solver = AsyncCaptchaAI(os.environ["CAPTCHAAI_API_KEY"])

    async with aiohttp.ClientSession() as session:
        # Fetch the page
        async with session.get(url) as resp:
            html = await resp.text()

        # Check if page has a CAPTCHA
        if "g-recaptcha" not in html:
            return html  # No CAPTCHA, return content

        # Solve the CAPTCHA
        token = await solver.solve(session, {
            "method": "userrecaptcha",
            "googlekey": site_key,
            "pageurl": url,
        })

        # Submit with solved token
        async with session.post(url, data={
            "g-recaptcha-response": token,
        }) as resp:
            return await resp.text()

Semaphore để kiểm soát tỷ lệ

Hạn chế các giải pháp đồng thời để tránh áp đảo API:

async def solve_with_limit(urls, site_key, max_concurrent=10):
    solver = AsyncCaptchaAI(os.environ["CAPTCHAAI_API_KEY"])
    semaphore = asyncio.Semaphore(max_concurrent)

    async def solve_one(session, url):
        async with semaphore:
            return await solver.solve(session, {
                "method": "userrecaptcha",
                "googlekey": site_key,
                "pageurl": url,
            })

    async with aiohttp.ClientSession() as session:
        tasks = [solve_one(session, url) for url in urls]
        results = await asyncio.gather(*tasks, return_exceptions=True)

    solved = sum(1 for r in results if not isinstance(r, Exception))
    print(f"Solved {solved}/{len(urls)} CAPTCHAs")
    return results

Ví dụ về cửa quay

async def solve_turnstile(url, sitekey):
    solver = AsyncCaptchaAI(os.environ["CAPTCHAAI_API_KEY"])

    async with aiohttp.ClientSession() as session:
        token = await solver.solve(session, {
            "method": "turnstile",
            "sitekey": sitekey,
            "pageurl": url,
        })
        return token

Khắc phục sự cố

Lỗi nguyên nhân sửa chữa
ClientConnectorError Sự cố mạng Kiểm tra kết nối
Submit failed: ERROR_ZERO_BALANCE Không có tiền Nạp tiền vào tài khoản
TimeoutError Giải quyết chậm Tăng tham số thời gian chờ
RuntimeError: Event loop is closed Sử dụng asyncio.run trong Jupyter Sử dụng nest_asyncio

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

Tại sao lại là aiohttp thay vì httpx?

aiohttp là thư viện HTTP không đồng bộ hoàn thiện nhất trong Python với hiệu suất tốt nhất cho khối lượng công việc có tính đồng thời cao. httpx cũng hoạt động - hãy xemhướng dẫn tích hợp httpx.

Tôi có thể chạy bao nhiêu giải pháp đồng thời?

CaptchaAI xử lý hơn 100 yêu cầu đồng thời. Sử dụng một semaphore để kiểm soát sự tương tranh của bạn dựa trên nhu cầu và số dư của bạn.

Tôi có thể sử dụng lại phiên trên nhiều giải pháp không?

Có, và bạn nên làm vậy. Phiên aiohttp duy trì nhóm kết nối, giúp các yêu cầu tiếp theo nhanh hơn.

Hướng dẫn liên quan

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