我如何以编程方式将captcha图像以python格式(给定html)上传到此解决服务?

我如何以编程方式将captcha图像以python格式(给定html)上传到此解决服务?,python,html,selenium,selenium-webdriver,2captcha,Python,Html,Selenium,Selenium Webdriver,2captcha,我正在编写一个python程序,需要能够在网站上解决CAPTCHA问题。我想用2captcha。我已经使用selenium编写了一个python脚本,除了解决CAPTCHA之外,它将完成我需要做的所有事情。当我点击2captcha.com“API”选项卡时,显示如下(连同其他参数): 您可以上传两种可用格式的验证码: 多部分和基本64.: 多页示例: <form method="post" action="http://2captcha.com/in.php" enctype="multi

我正在编写一个python程序,需要能够在网站上解决CAPTCHA问题。我想用2captcha。我已经使用selenium编写了一个python脚本,除了解决CAPTCHA之外,它将完成我需要做的所有事情。当我点击2captcha.com“API”选项卡时,显示如下(连同其他参数):

您可以上传两种可用格式的验证码:

多部分和基本64.:

多页示例:

<form method="post" action="http://2captcha.com/in.php" enctype="multipart/form-data">
<input type="hidden" name="method" value="post">
Your key:
<input type="text" name="key" value="YOUR_APIKEY">
The CAPTCHA file:
<input type="file" name="file">
<input type="submit" value="download and get the ID">
</form>
<form method="post" action="http://2captcha.com/in.php">
<input type="hidden" name="method" value="base64">
Your key:
<input type="text" name="key" value="YOUR_APIKEY">
The CAPTCHA file body in base64 format:
<textarea name="body">BASE64_FILE</textarea>
<input type="submit" value="download and get the ID">
</form>

你的钥匙:
验证码文件:
您的_APIKEY-是32个符号长度的密钥

Base64示例:

<form method="post" action="http://2captcha.com/in.php" enctype="multipart/form-data">
<input type="hidden" name="method" value="post">
Your key:
<input type="text" name="key" value="YOUR_APIKEY">
The CAPTCHA file:
<input type="file" name="file">
<input type="submit" value="download and get the ID">
</form>
<form method="post" action="http://2captcha.com/in.php">
<input type="hidden" name="method" value="base64">
Your key:
<input type="text" name="key" value="YOUR_APIKEY">
The CAPTCHA file body in base64 format:
<textarea name="body">BASE64_FILE</textarea>
<input type="submit" value="download and get the ID">
</form>

你的钥匙:
base64格式的验证码文件正文:
BASE64_文件
您的_APIKEY-是32个符号长度的密钥

BASE64_文件-是BASE64编码的图像主体

我对python及其大部分科学和数学模块都很了解,但我对web相关编程有点陌生。上面的代码看起来像html。如何让python程序执行上述html指令?

查看模块

看看这个模块


我是2captcha团队的,我们有一些python示例。 您可以使用以下代码:

"""
这是如何通过GoogleReCAPTCHAV2的示例。 我已经使用python+selenium+phantomJS来实现这一点。 Phantom JS是用于自动化web测试的无头浏览器。我用它来废弃网页,绕过谷歌验证码。 要使用它,您应该安装phantomJS(并在linux系统案例中从源代码构建)和selenium python模块

谷歌验证码出现在iframes的页面上。您应该废弃它的元素,单击图像并在单击“确定”后检查错误消息

“”“

从selenium导入webdriver
从selenium.webdriver.common.keys导入密钥
从selenium.common.exceptions导入*
进口稀土
导入操作系统
导入系统
导入时间
随机输入
导入请求
从PIL导入图像
类capcha_解析器:
def uuu init uuu(self,captchakey,proxy=None):
"""
假设您已经在linux系统的/bin文件夹中安装了phantomjs。
"""
self.TWOCAPTCHA\u API\u KEY=captchakey
幻影参数=[]
如果代理:
self.PROXY=PROXY
phantom_args=['--proxy='+self.proxy'--proxy type=http','--proxy type=https']
self.driver=webdriver.PhantomJS('/bin/PhantomJS',service_args=phantom_args)
self.driver.set\u page\u load\u超时(20)
def失败(自我,消息):
打印“[!]错误:“+msg”
self.driver.save_屏幕截图('error.png'))
def get_页面(自我):
self.driver.gethttps://www.google.com/recaptcha/api2/demo')
self.driver.save_屏幕截图('page.png')
返回0
def send_capcha(自身,文件名):
数字=[]
captchafile={'file':打开(文件名'rb')}
数据={'key':self.TWOCAPTCHA_API_key,'method':'post'}
r=请求。post('http://2captcha.com/in.php,files=captchafile,data=data)
如果r.ok和r.text.find('ok')>-1:
reqid=r.text[r.text.find(“|”)+1:]
打印“[+]Capcha id:”+REQUID
对于范围(40)内的超时:
r=请求。获取('http://2captcha.com/res.php?key={0}&action=get&id={1}'。格式(self.TWOCAPTCHA\u API\u KEY,reqid))
如果r.text.find('CAPCHA\u NOT\u READY')>-1:
打印r.text
时间。睡眠(3)
如果r.text.find('ERROR')>-1:
返回[]
如果r.text.find('OK')>-1:
返回列表(r.text[r.text.find(“|”)+1:]
返回[]
def旁路\u验证码(自身):
"""
Google recaptcha可以通过id找到。带有复选框的框架的id以I0开头,recapcha框架的id以I1开头
"""
capthcaboxframe=self.driver.find_element_by_xpath('//iframe[以(@id,“I0”)]开头])
自驱动程序切换到帧(capthcaboxframe)
时间。睡眠(1)
checkbox=self.driver.find_element_by_id('recaptcha-anchor'))
复选框。单击()
打印“[*]点击复选框”
时间。睡眠(2)
self.driver.switch_to.default_content()
capcthaframe=self.driver.find_element_by_xpath('//iframe[以(@id,“I1”)]开头])
边界框=(
capcthaframe.位置['x'],左
盖轴框架位置['y'],#上部
(capcthaframe.location['x']+capcthaframe.size['width']),右
(capcthaframe.location['y']+capcthaframe.size['height'])底部
imgname='capcha.jpeg'#使用jpeg,因为png图像可能超过2capcha文件大小限制
时间。睡眠(2)
self.driver.save_屏幕截图(imgname)
base_image=image.open(imgname)
裁剪的图像=基础图像。裁剪(边界框)
base\u image=base\u image.resize(裁剪的\u image.size)
基本图片。粘贴(裁剪图片,(0,0))
基本图像保存(imgname)
数字=自我发送(imgname)
如果数字=[]:
返回-1
自驱动开关至机架(盖轴机架)
picturetable=self.driver.find_element_by_css_选择器('.rc-imageselect-table-3'))
图像=[]
对于picturetable中的行。通过标签名称(“tr”)查找元素:
对于行中的列。通过标记名称(“td”)查找元素:
images.append(col.find_element_by_tag_name('img'))
如果图像==[]:
self.fail(“未找到验证码图像”)
返回-1
打印“[*]得到答案:“+str(数字)
对于数字中的数字:
索引=整数(数字)-1
图像[索引]。单击()
打印“[+]点击图像”+str(索引)
self.driver.save_屏幕截图('res.png')
verifybutton=self.driver.find_element_by_id('recaptcha-verify-button'))
验证按钮。单击()
打印“[*]单击验证按钮”
时间。睡眠(2)
如果self.driver.f