验证码和Python请求几乎是新的。验证码文档说要复制data-sitekey
参数的值。
这是我的尝试,使用Selenium打开url并使用Pythonrequests
来获得响应。
mainurl = 'https://imagetyperz.xyz/automation/recaptcha-v2.html'
driver.get(mainurl)
data_sitekey_class = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CLASS_NAME, "g-recaptcha"))).get_attribute("data-sitekey")
print(data_sitekey_class)
src_css_selector = driver.find_element_by_css_selector("iframe[role='presentation']").get_attribute("src")
print(src_css_selector)
keygoogle = src_css_selector[52:92]
print('Site Key = ', keygoogle)
data_post = {'key': data_sitekey_class, 'method': 'userrecaptcha', 'googlekey': keygoogle, "pageurl": mainurl}
response = requests.post(url = 'https://2captcha.com/in.php', data = data_post )
print(response)
print(response.text)
我得到200
作为回应:
6LdXeIYUAAAAAFmFKJ6Cl3zo4epRZ0LDdOrYsvRY
https://www.google.com/recaptcha/api2/anchor?ar=1&k=6LdXeIYUAAAAAFmFKJ6Cl3zo4epRZ0LDdOrYsvRY&co=aHR0cHM6Ly9pbWFnZXR5cGVyei54eXo6NDQz&hl=en&v=vJuUWXolyYJx1oqUVmpPuryQ&size=normal&cb=r14cgu7t25ul
Site Key = 6LdXeIYUAAAAAFmFKJ6Cl3zo4epRZ0LDdOrYsvRY
<Response [200]>
ERROR_WRONG_USER_KEY
这是由于:
ERROR_WRONG_USER_KEY
此外,错误部分提到:
Error code: ERROR_WRONG_USER_KEY
Description: You've provided key parameter value in incorrect format, it should contain 32 symbols.
Action: Stop sending requests. Check your API key.
最后,解决验证码部分提到:
从您的帐户设置页面获取您的 API 密钥。每个用户都有一个唯一的身份验证令牌,我们称之为API密钥。这是一个 32 个字符的字符串,如下所示:
1abc234de56fab7c89012d34e56fa7b8
我看到的data-sitekey
是:
6LdXeIYUAAAAAFmFKJ6Cl3zo4epRZ0LDdOrYsvRY
这是41位。
我哪里出错了?
data-sitekey
用 41 个字符的字符串表示就可以了。发生错误ERROR_WRONG_USER_KEY
因为我从一开始就没有准备好有效的 API 密钥。即使您的帐户余额为零,您也可以成功获取带有文本ERROR_ZERO_BALANCE
<Response [200]>
,如下所示:
-
代码块:
from selenium import webdriver from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC import requests options = webdriver.ChromeOptions() options.add_argument("start-maximized") options.add_experimental_option("excludeSwitches", ["enable-automation"]) options.add_experimental_option('useAutomationExtension', False) driver = webdriver.Chrome(options=options, executable_path=r'C:WebDriverschromedriver.exe') mainurl = 'https://imagetyperz.xyz/automation/recaptcha-v2.html' driver.get(mainurl) data_sitekey = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CLASS_NAME, "g-recaptcha"))).get_attribute("data-sitekey") print(data_sitekey) api_key = '--------------------------------' data_post = {'key': api_key, 'method': 'userrecaptcha', 'googlekey': data_sitekey, "pageurl": mainurl} response = requests.post(url = 'https://2captcha.com/in.php', data = data_post ) print(response) print(response.text)
-
控制台输出:
6LdXeIYUAAAAAFmFKJ6Cl3zo4epRZ0LDdOrYsvRY <Response [200]> ERROR_ZERO_BALANCE
有同样的错误。我使用的是headers=header
而不是data=header
.