07 验证码处理

引入:

  • 相关的门户网站在进行登录的时候,如果用户连续登录的次数超过3次或者5次的时候,就会在登录页中动态生成验证码。通过验证码达到分流和反爬的效果。

今日概要:

  • 使用云打码平台识别验证码

  云打码官网地址: http://www.yundama.com/

    云打码使用流程:

      注册:普通用户和开发者用户(两个都要注册) 

      登陆:

        —  普通用户:查询余额

        —  开发者用户: 

            -  创建一个软件:我的软件  -->  添加一个软件

            -  下载示例代码:点击开发文档  ->  调用示例及最新的DLL  -> 点击PythonHttp示例下载,即可下载

     开发者用户下载PythonHttp示例使用:

      1. 解压下载好的PythonHttp调用示例,文件中有包含的验证图片,YDMHTTPDemo2.x.py,YDMHTTPDemo2.x.p3 三个文件

      2. 使用时,将验证图片 和 YDMHTTPDemo2.x.p3 内容导入到项目即可使用

       
  1 import http.client, mimetypes, urllib, json, time, requests
  2 
  3 ######################################################################
  4 
  5 class YDMHttp:
  6 
  7     apiurl = 'http://api.yundama.com/api.php'
  8     username = ''
  9     password = ''
 10     appid = ''
 11     appkey = ''
 12 
 13     def __init__(self, username, password, appid, appkey):
 14         self.username = username  
 15         self.password = password
 16         self.appid = str(appid)
 17         self.appkey = appkey
 18 
 19     def request(self, fields, files=[]):
 20         response = self.post_url(self.apiurl, fields, files)
 21         response = json.loads(response)
 22         return response
 23     
 24     def balance(self):
 25         data = {'method': 'balance', 'username': self.username, 'password': self.password, 'appid': self.appid, 'appkey': self.appkey}
 26         response = self.request(data)
 27         if (response):
 28             if (response['ret'] and response['ret'] < 0):
 29                 return response['ret']
 30             else:
 31                 return response['balance']
 32         else:
 33             return -9001
 34     
 35     def login(self):
 36         data = {'method': 'login', 'username': self.username, 'password': self.password, 'appid': self.appid, 'appkey': self.appkey}
 37         response = self.request(data)
 38         if (response):
 39             if (response['ret'] and response['ret'] < 0):
 40                 return response['ret']
 41             else:
 42                 return response['uid']
 43         else:
 44             return -9001
 45 
 46     def upload(self, filename, codetype, timeout):
 47         data = {'method': 'upload', 'username': self.username, 'password': self.password, 'appid': self.appid, 'appkey': self.appkey, 'codetype': str(codetype), 'timeout': str(timeout)}
 48         file = {'file': filename}
 49         response = self.request(data, file)
 50         if (response):
 51             if (response['ret'] and response['ret'] < 0):
 52                 return response['ret']
 53             else:
 54                 return response['cid']
 55         else:
 56             return -9001
 57 
 58     def result(self, cid):
 59         data = {'method': 'result', 'username': self.username, 'password': self.password, 'appid': self.appid, 'appkey': self.appkey, 'cid': str(cid)}
 60         response = self.request(data)
 61         return response and response['text'] or ''
 62 
 63     def decode(self, filename, codetype, timeout):
 64         cid = self.upload(filename, codetype, timeout)
 65         if (cid > 0):
 66             for i in range(0, timeout):
 67                 result = self.result(cid)
 68                 if (result != ''):
 69                     return cid, result
 70                 else:
 71                     time.sleep(1)
 72             return -3003, ''
 73         else:
 74             return cid, ''
 75 
 76     def report(self, cid):
 77         data = {'method': 'report', 'username': self.username, 'password': self.password, 'appid': self.appid, 'appkey': self.appkey, 'cid': str(cid), 'flag': '0'}
 78         response = self.request(data)
 79         if (response):
 80             return response['ret']
 81         else:
 82             return -9001
 83 
 84     def post_url(self, url, fields, files=[]):
 85         for key in files:
 86             files[key] = open(files[key], 'rb');
 87         res = requests.post(url, files=files, data=fields)
 88         return res.text
 89 
 90 ######################################################################
 91 
 92 # 用户名  (指的是普通用户的用户名和密码)
 93 username    = 'username'
 94 
 95 # 密码
 96 password    = 'password'                            
 97 
 98 # 软件ID,开发者分成必要参数。登录开发者后台【我的软件】获得!
 99 appid       = 1                                     
100 
101 # 软件密钥,开发者分成必要参数。登录开发者后台【我的软件】获得!
102 appkey      = '22cc5376925e9387a23cf797cb9ba745'    
103 
104 # 图片文件
105 filename    = 'getimage.jpg'                        
106 
107 # 验证码类型,# 例:1004表示4位字母数字,不同类型收费不同。请准确填写,否则影响识别率。在此查询所有类型 http://www.yundama.com/price.html
108 codetype    = 1004
109 
110 # 超时时间,秒
111 timeout     = 60                                    
112 
113 # 检查
114 if (username == 'username'):
115     print('请设置好相关参数再测试')
116 else:
117     # 初始化
118     yundama = YDMHttp(username, password, appid, appkey)
119 
120     # 登陆云打码
121     uid = yundama.login();
122     print('uid: %s' % uid)
123 
124     # 查询余额
125     balance = yundama.balance();
126     print('balance: %s' % balance)
127 
128     # 开始识别,图片路径,验证码类型ID,超时时间(秒),识别结果
129     cid, result = yundama.decode(filename, codetype, timeout);
130     print('cid: %s, result: %s' % (cid, result))
131 
132 ######################################################################
YDMHTTPDemo2.x.p3 具体代码

云打码平台处理验证码的实现流程:

- 1.对携带验证码的页面数据进行抓取
- 2.可以将页面数据中验证码进行解析,验证码图片下载到本地
- 3.可以将验证码图片提交给三方平台进行识别,返回验证码图片上的数据值
    - 云打码平台:
        - 1.在官网中进行注册(普通用户和开发者用户)
        - 2.登录开发者用户:
            - 1.实例代码的下载(开发文档-》调用实例及最新的DLL-》PythonHTTP实例下载)
            - 2.创建一个软件:我的软件-》添加新的软件
        -3.使用示例代码中的源码文件中的代码进行修改,让其识别验证码图片中的数据值

      

      
  

猜你喜欢

转载自www.cnblogs.com/mwhylj/p/10244004.html