2017-04-10 254 views
0

我使用请求模块创建了一个web机器人。我需要通过一个有recaptcha的表单。我有一个本地网站,可以为这个rec​​aptcha生成一个g-captcha-response。我想知道g-captcha-response是否是通过recaptcha所必需的唯一后置参数。如果不是,我还需要发布哪些其他信息?用Python请求处理Google Recaptcha

这里是我的代码:

CaptchaKey = # g-captcha-response from my local website 

Session = requests.Session() 
FormData = { 
    'g-captcha-response': CaptchaKey 
} 
Session.post(SubmitURL, data=FormData) 

回答

0

reCaptcha documentation这里给出了一个相当不错的转向。使用Python和请求,你会在最基本的东西中看到如下所示的内容。

import requests, json 

try: 
    content = requests.post(
     'https://www.google.com/recaptcha/api/siteverify', 
     data={ 
      'secret': RECAPTCHA_SECRET, 
      'response': captcha_response_from_form, 
      'remoteip': USER_IP_ADDRESS, 
     } 
    ).content 
except ConnectionError: # Handle fundamental connectivity issues 
    # Handle your error state 

# Will throw ValueError if we can't parse Google's response 
content = json.loads(content) 

if not 'success' in content or not content['success']: 
    # The reCaptcha hasn't passed...