在Selenium Chrome Session(2Captcha)中使用Python重新编写解决方案

时间:2018-07-11 09:23:25

标签: python google-chrome selenium recaptcha

我想使用Selenium模块通过Python解决网站上的ReCaptcha。

我的问题是:我如何从Selenium Chrome浏览器中发送2Captcha请求,该请求可以解析ReCaptcha并验证Selenium Chrome页面中的成功结果?

我希望脚本在解决后单击“提交”按钮,以便我可以继续执行Selenium脚本。

下面的代码成功解析了ReCaptcha,但是它不在Selenium中。

import requests
from time import sleep

# Add these values
API_KEY = 'FILLINAPIKEYHERE'  # Your 2captcha API KEY
site_key = 'FILLINTHEPAGE'SRECAPTCHAKEYHERE'  # site-key, read the 2captcha docs on how to get this
url = 'THEWEBPAGETHATNEEDSRESOLVEMENT'  # example url
proxy = 'PROXY:PORTINHERE'  # example proxy

proxy = {'http': 'http://' + proxy, 'https': 'https://' + proxy}

s = requests.Session()

# here we post site key to 2captcha to get captcha ID (and we parse it here too)
captcha_id = s.post("http://2captcha.com/in.php?key={}&method=userrecaptcha&googlekey={}&pageurl={}".format(API_KEY, site_key, url), proxies=proxy).text.split('|')[1]
# then we parse gresponse from 2captcha response
recaptcha_answer = s.get("http://2captcha.com/res.php?key={}&action=get&id={}".format(API_KEY, captcha_id), proxies=proxy).text
print("solving ref captcha...")
while 'CAPCHA_NOT_READY' in recaptcha_answer:
    sleep(5)
    recaptcha_answer = s.get("http://2captcha.com/res.php?key={}&action=get&id={}".format(API_KEY, captcha_id), proxies=proxy).text
recaptcha_answer = recaptcha_answer.split('|')[1]

# we make the payload for the post data here, use something like mitmproxy or fiddler to see what is needed
payload = {
    'key': 'value',
    'gresponse': recaptcha_answer  # This is the response from 2captcha, which is needed for the post request to go through.
    }


# then send the post request to the url
response = s.post(url, payload, proxies=proxy)

# And that's all there is to it other than scraping data from the website, which is dynamic for every website.

预先感谢,希望有人能帮助我或为我指明正确的方向。

0 个答案:

没有答案