Cookie使用通知

本网站会使用cookies,以便记住您的选择,并提供个性化服务。了解更多

如果网站速度慢,请使用网站链接 2captcha.cn
使用Python绕过验证码

使用Python绕过验证码

集成Python程序包,自动破解任何网站的验证码。

Python程序包可轻松集成2Captcha的验证码破解服务API,以绕过reCAPTCHAhCaptchaArkose captchaGeetest,并破解其他验证码。

快速开始

安装

脚本包可通过安装程序安装或人工安装

Pip

您需要使用Pip来自动安装库包,这是Python程序包的标准解决方案。前往链接下载并按指示安装。安装完成后,启用命令:

pip3 install 2captcha-python

欢迎您了解GitHub库,找到可轻松集成API的库和模块。

配置

配置已安装软件包的所有必要参数说明

TwoCaptcha类实例可按照如下方法创建:

from twocaptcha import TwoCaptcha

solver = TwoCaptcha('YOUR_API_KEY')

您还能为已创建的实例设置选项:

config = {
            'server':           '2captcha.com',
            'apiKey':           'YOUR_API_KEY',
            'softId':            123,
            'callback':         'https://your.site/result-receiver',
            'defaultTimeout':    120,
            'recaptchaTimeout':  600,
            'pollingInterval':   10,
        }
solver = TwoCaptcha(**config)
TwoCaptcha实例选项
选项默认值说明
服务器‘2captcha.com’API服务器。您可以将注册账户设置为‘rucaptcha.com’。
softId-您将在2Captcha软件目录发布后获得软件ID
回调函数-验证码识别结果会发送至网络服务器URL,但应先在账户的pingback设置中注册URL
defaultTimeout120除reCAPTCHA外的验证码的轮询超时时间(秒),用于判定模块尝试从res.phpAPI端点获得答案的时间
recaptchaTimeout600reCAPTCHA的轮询超时时间(秒),用于判定模块尝试从res.phpAPI端点获得答案的时间
pollingInterval10res.phpAPI端点发送请求的间隔时间(秒),不建议设置在5秒以内
重要提示:一旦回调函数确定用于TwoCaptcha实例,那么所有方法都只返回验证码ID,无法通过轮询API获得结果。结果将发送至回调URL。请通过getResult方法进行人工破解。

破解验证码

您在提交图片验证码时可提出额外选项,以便2Captcha的员工能够正确破解。

验证码选项
选项默认值说明
numeric0判定验证码是否由数字或其他符号组成,详情请见API文档
minLength0最小答案长度
maxLength0最大答案长度
phrase0判定答案是否由多个词语组成
caseSensitive0判定答案是否区分大小写
calc0确定验证码需要计算
lang-确定验证码语言,见可用语言列表
hintImg-所有验证码员工可见的提示图片
hintText-员工可见的验证码提示或任务文字
simpleCaptcha

您可以利用以下方法绕过normal captcha(带有扭曲文字的图片)。这个方法也可用于识别图片上的任何文字。

result = solver.normal('path/to/captcha.jpg', param1=..., ...)
textCaptcha

这种方法可用于绕过需要回答清晰文字问题的验证码。

result = solver.text('If tomorrow is Saturday, what day is today?', param1=..., ...)
recaptchaV2

此方法可破解reCAPTCHA V2,并获得令牌实现绕过保护。

result = solver.recaptcha(sitekey='6Le-wvkSVVABCPBMRTvw0Q4Muexq1bi0DJwx_mJ-',
  url='https://mysite.com/page/with/recaptcha',
  param1=..., ...)
recaptchaV3

此方法利可破解reCAPTCHA V3,并返回令牌。

result = solver.recaptcha(sitekey='6Le-wvkSVVABCPBMRTvw0Q4Muexq1bi0DJwx_mJ-',
  url='https://mysite.com/page/with/recaptcha',
  version='v3',
  param1=..., ...)
funCaptcha

FunCaptcha(Arkoselabs)破解方法,并返回令牌。

result = solver.funcaptcha(sitekey='6Le-wvkSVVABCPBMRTvw0Q4Muexq1bi0DJwx_mJ-',
  url='https://mysite.com/page/with/funcaptcha',
  param1=..., ...)
geeTest

此方法可破解GeeTest拼图验证码,并返回一组JSON格式的令牌。

result = solver.geetest(gt='f1ab2cdefa3456789012345b6c78d90e',
  challenge='12345678abc90123d45678ef90123a456b',
  url='https://www.site.com/page/',
  param1=..., ...)
hCaptcha

此方法可破解hCaptcha,并返回可以绕过验证码的令牌。

result = solver.hcaptcha(sitekey='10000000-ffff-ffff-ffff-000000000001',
  url='https://www.site.com/page/', 
  param1=..., ...)
keyCaptcha

通过令牌形式破解KeyCaptcha。

result = solver.keycaptcha(s_s_c_user_id=10,
    s_s_c_session_id='493e52c37c10c2bcdf4a00cbc9ccd1e8',
    s_s_c_web_server_sign='9006dc725760858e4c0715b835472f22-pz-',
    s_s_c_web_server_sign2='2ca3abe86d90c6142d5571db98af6714',
    url='https://www.keycaptcha.ru/demo-magnetic/', 
    param1=..., ...)
capy

以令牌形式绕过Capy拼图验证码。

result = solver.capy(sitekey='PUZZLE_Abc1dEFghIJKLM2no34P56q7rStu8v',
  url='http://mysite.com/',
  api_server='https://jp.api.capy.me/',
  param1=..., ...)
grid

Grid法最初名为Old reCAPTCHA V2法,先在图中画好网格,点击特定网格框,以绕过任何类型的验证码。这种方法会返回方框数。

result = solver.grid('path/to/captcha.jpg', param1=..., ...)
canvas

Canvas法需要围着图中物体画一条线。这种方法会返回一组点坐标,用于绘制多边形。

result = solver.canvas('path/to/captcha.jpg', param1=..., ...)
clickCaptcha

ClickCaptcha会返回验证码图片的点坐标,若您需要点击图片的特定点,就可以使用这种方法。

result = solver.coordinates('path/to/captcha.jpg', param1=..., ...)
rotateCaptcha

这种方法可破解需要旋转物体的验证码,主要用于绕过FunCaptcha。它会返回旋转角度。

result = solver.rotate('path/to/captcha.jpg', param1=..., ...)

其他方法

主要脚本执行时可用的其他方法

send / getResult

上述方法可用于人工提交验证码和答案轮询。

import time
. . . . . 

id = solver.send(file='path/to/captcha.jpg')
time.sleep(20)

code = solver.get_result(id)

balance

以此方法获取账户余额。

balance = solver.balance()

report

以此方法报告验证码答案之优劣。

solver.report(id, True) # captcha solved correctly
solver.report(id, False) # captcha solved incorrectly

错误处理

网站在处理请求时返回的标准错误的可能变体

如发生错误,验证码破解程序会提示异常。妥善处理这类情况很重要。我们推荐使用try/except来处理异常。

Try:
    result = solver.text('If tomorrow is Saturday, what day is today?')
Except ValidationException as e:
    # invalid parameters passed
  print(e)
Except NetworkException as e:
  # network error occurred
  print(e)
Except ApiException as e:
  # api respond with error
  print(e)
Except TimeoutException as e:
  # captcha is not solved so far
  print(e)

代理

您可以将代理作为参数应用于以下方法:recaptcha、funcaptcha、geetest。代理将被转发至API以破解验证码。

proxy={
    'type': 'HTTPS',
    'uri': 'login:password@IP_address:PORT'
}

异步回调

您也可以利用asyncio进行异步回调。

import asyncio
import concurrent.futures
from twocaptcha import TwoCaptcha

captcha_result = await captchaSolver(image)

async def captchaSolver(image):
    loop = asyncio.get_running_loop()
    with concurrent.future.ThreadPoolExecutor() as pool:
        result = await loop.run_in_executor(pool, lambda: TwoCaptcha(API_KEY).normal(image))
        return result

用于网站API集成的其他语言

Chrome和Firefox浏览器均可使用验证码绕过扩展程序验证码绕过 扩展程序建议扩展
备用扩展
CaptchaTheCat