如何在程序中处理reCAPTCHA?

可以使用人工打码平台进行破解:

http://deathbycaptcha.com

 

本文不是讲如何破解谷歌的reCAPTCHA(实际上我们也办不到),而是介绍在程序中借助第三方(人工)打码平台顺利通过reCAPTCHA验证。
由于使用人工打码会产生费用,并且费用是和调用次数成正比的,所以本方法仅适用于reCAPTCHA出现频率比较低的场景,例如:
1)网站登录使用了reCAPTCHA。比如,有时Linkedin登录就会出现reCAPTCHA(如下图所示),验证当前客户端是否是“真人”。

2)网站对访问频率过快的客户端返回reCAPTCHA,通过验证后即可继续访问。比如Zocdoc.com这个网站。

下面进入正题。如何在程序中借助第三方(人工)打码平台通过reCAPTCHA验证?
因为reCAPTCHA表单是JS动态创建的,我们遇到的第一个难题就是如何获取到reCAPTCHA表单中的验证码图片的路径及各隐藏表单域的值。
1)一个办法就是使用类似webkit的浏览器模拟工具(例如,phantomjs)加载页面,这样就能直接获取到JS生成的HTML源码。但是实现起来比较复杂,还需要借助第三方的软件。
2)查看源码会发现如果页面禁用了JS,reCAPTCHA将使用iframe模式加载(如下示),此时验证码图片路径和各表单项都是直接可见的。

<noscript> <iframe src="https://www.google.com/recaptcha/api/noscript?k=******" height="300" width="500" frameborder="0"></iframe> <br><textarea name="recaptcha_challenge_field" rows="3" cols="40"></textarea> </noscript>

所以我们直接请求“https://www.google.com/recaptcha/api/noscript?k=******”即可获取到验证码的图片路径和其它隐藏表单域的值。
这个问题解决了。接下来我们要获取到验证码图片对应的明文。如何调用第三方打码平台进行图片验证码识别呢?
我们需要先下载验证码图片,再把图片的二进制数据上传给打码平台,然后等待平台人工打码返回明文。打码平台一般都提供了供各种常见语言调用的API,所以该过程也比较简单。
现在我们已经获取到验证码图片对应的明文,只要我们将其和其它隐藏表单域参数一起提交就能完成验证过程了。

闲话不多说,还是直接上代码比较实在(Python实现, 这是我们真实项目中用到的):

  1. # coding: utf-8  
  2. # recaptcha.py  
  3.   
  4. import sys  
  5. import os  
  6. os.chdir(os.path.dirname(os.path.realpath(sys.argv[0])))  
  7. import re  
  8. import StringIO  
  9. import deathbycaptcha  
  10. from urlparse import urljoin  
  11. from webscraping import common, xpath  
  12.   
  13. DEATHBYCAPTCHA_USERNAME = '******'  
  14. DEATHBYCAPTCHA_PASSWORD = '******'  
  15.   
  16. def read_captcha(image):  
  17.     """image - fileobj, the captcha to be recognized 
  18.     """  
  19.     client = deathbycaptcha.SocketClient(DEATHBYCAPTCHA_USERNAME, DEATHBYCAPTCHA_PASSWORD)  
  20.     try:  
  21.         balance = client.get_balance()  
  22.         # Put your CAPTCHA file name or file-like object, and optional  
  23.         # solving timeout (in seconds) here:  
  24.         common.logger.info('Submit captcha to http://deathbycaptcha.com/.')  
  25.         captcha = client.decode(image)  
  26.         if captcha:  
  27.             # The CAPTCHA was solved; captcha["captcha"] item holds its  
  28.             # numeric ID, and captcha["text"] item its text.  
  29.             common.logger.info("CAPTCHA %s solved: %s" % (captcha["captcha"], captcha["text"]))  
  30.             return captcha["text"].strip()  
  31.     except deathbycaptcha.AccessDeniedException:  
  32.         # Access to DBC API denied, check your credentials and/or balance  
  33.         common.logger.info('Access to DBC API denied, check your credentials and/or balance')  
  34.   
  35. def solve_recaptcha(html, D):  
  36.     """To solve the Google recaptcha 
  37.     """  
  38.     m = re.compile(r']+src="(https?://www\.google\.com/recaptcha/api/noscript\?k=[^"]+)"', re.IGNORECASE).search(html)  
  39.     if m:  
  40.         common.logger.info('Need to solve the recaptcha.')  
  41.         # need to solve the captcha first  
  42.         iframe_url = m.groups()[0]  
  43.         # load google recaptcha page  
  44.         iframe_html = D.get(iframe_url, read_cache=False)  
  45.         # extract recaptcha_challenge_field value for future use  
  46.         recaptcha_challenge_field = xpath.get(iframe_html, '//input[@id="recaptcha_challenge_field"]/@value')  
  47.         if recaptcha_challenge_field:  
  48.             # extract captcha image link  
  49.             captcha_image_url = xpath.get(iframe_html, '//img/@src')  
  50.             if captcha_image_url:  
  51.                 captcha_image_url = urljoin(iframe_url, captcha_image_url)  
  52.                 # download captcha  
  53.                 captcha_bytes = D.get(captcha_image_url, read_cache=False)  
  54.                 if captcha_bytes:  
  55.                     #open('captcha.jpg', 'wb').write(captcha_bytes)  
  56.                     fileobj = StringIO.StringIO(captcha_bytes)  
  57.                     # read the captcha via deathbycaptcha  
  58.                     recaptcha_response_field = read_captcha(fileobj)  
  59.                     if recaptcha_response_field:  
  60.                         common.logger.info('Have got the captcha content = "%s".' % str(recaptcha_response_field))  
  61.                         url = 'https://www.linkedin.com/uas/captcha-submit'  
  62.                         post_data = {}  
  63.                         captcha_form = xpath.get(html, '//form[@name="captcha"]')  
  64.                         for input_name, input_value in re.compile(r']+type="hidden"\s+name="([^<>\"]+)"\s+value="([^<>\"]+)"').findall(captcha_form):  
  65.                             post_data[input_name] = input_value  
  66.                         post_data['recaptcha_challenge_field'] = recaptcha_challenge_field  
  67.                         post_data['recaptcha_response_field'] = recaptcha_response_field                         
  68.                         return D.get(url, data=post_data, read_cache=False)  

猜你喜欢

转载自jsczxy2.iteye.com/blog/2248586