驗證碼處理 -- 爬蟲

引入php

  • 相關的門戶網站在進行登陸的時候,若是用戶連續登陸的次數超過3次或者5次的時候,就會在登陸頁中動態生成驗證碼。經過驗證碼達到分流和反爬的效果
  • - 1.對攜帶驗證碼的頁面數據進行抓取
  • - 2.能夠將頁面數據中驗證碼進行解析,驗證碼圖片下載到本地
  • - 3.能夠將驗證碼圖片提交給三方平臺進行識別,返回驗證碼圖片上的數據值
  • - 雲打碼平臺:(具體雲代碼平臺接口代碼見百度雲,連接: https://pan.baidu.com/s/1utwt70743u2PJP4BErq3ZQ 提取碼: kwtu 複製這段內容後打開百度網盤手機App,操做更方便哦)

具體實現代碼以下:html

 1 import http.client, mimetypes, urllib, json, time, requests
 2 
 3 
 4 class YDMHttp:
 5     apiurl = 'http://api.yundama.com/api.php'
 6     username = ''
 7     password = ''
 8     appid = ''
 9     appkey = ''
10 
11     def __init__(self, username, password, appid, appkey):
12         self.username = username
13         self.password = password
14         self.appid = str(appid)
15         self.appkey = appkey
16 
17     def request(self, fields, files=[]):
18         response = self.post_url(self.apiurl, fields, files)
19         response = json.loads(response)
20         return response
21 
22     def balance(self):
23         data = {'method': 'balance', 'username': self.username, 'password': self.password, 'appid': self.appid,
24                 'appkey': self.appkey}
25         response = self.request(data)
26         if (response):
27             if (response['ret'] and response['ret'] < 0):
28                 return response['ret']
29             else:
30                 return response['balance']
31         else:
32             return -9001
33 
34     def login(self):
35         data = {'method': 'login', 'username': self.username, 'password': self.password, 'appid': self.appid,
36                 '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,
48                 'appkey': self.appkey, 'codetype': str(codetype), 'timeout': str(timeout)}
49         file = {'file': filename}
50         response = self.request(data, file)
51         if (response):
52             if (response['ret'] and response['ret'] < 0):
53                 return response['ret']
54             else:
55                 return response['cid']
56         else:
57             return -9001
58 
59     def result(self, cid):
60         data = {'method': 'result', 'username': self.username, 'password': self.password, 'appid': self.appid,
61                 'appkey': self.appkey, 'cid': str(cid)}
62         response = self.request(data)
63         return response and response['text'] or ''
64 
65     def decode(self, filename, codetype, timeout):
66         cid = self.upload(filename, codetype, timeout)
67         if (cid > 0):
68             for i in range(0, timeout):
69                 result = self.result(cid)
70                 if (result != ''):
71                     return cid, result
72                 else:
73                     time.sleep(1)
74             return -3003, ''
75         else:
76             return cid, ''
77 
78     def report(self, cid):
79         data = {'method': 'report', 'username': self.username, 'password': self.password, 'appid': self.appid,
80                 'appkey': self.appkey, 'cid': str(cid), 'flag': '0'}
81         response = self.request(data)
82         if (response):
83             return response['ret']
84         else:
85             return -9001
86 
87     def post_url(self, url, fields, files=[]):
88         for key in files:
89             files[key] = open(files[key], 'rb')
90         res = requests.post(url, files=files, data=fields)
91         return res.text
封裝一個類YDMHttp

繼續封裝一個函數,調用這個類並執行其中的代碼:json

 1 def parse_code_img(username, password, appid, appkey, filename, codetype):
 2     # 用戶名
 3     username = username
 4     # 密碼
 5     password = password
 6     # 軟件ID,開發者分紅必要參數。登陸開發者後臺【個人軟件】得到!
 7     appid = appid
 8     # 軟件密鑰,開發者分紅必要參數。登陸開發者後臺【個人軟件】得到!
 9     appkey = appkey
10     # 圖片文件
11     filename = filename
12     # 驗證碼類型,# 例:1004表示4位字母數字,不一樣類型收費不一樣。請準確填寫,不然影響識別率。在此查詢全部類型 http://www.yundama.com/price.html
13     codetype = codetype
14     # 超時時間,秒
15     timeout = 30
16 
17     # 檢查
18     if (username == ''):
19         print('請設置好相關參數再測試')
20     else:
21         # 初始化
22         yundama = YDMHttp(username, password, appid, appkey)
23         # 登錄雲打碼
24         uid = yundama.login()
25         print('uid: %s' % uid)
26         # 查詢餘額
27         balance = yundama.balance()
28         print('balance: %s' % balance)
29         # 開始識別,圖片路徑,驗證碼類型ID,超時時間(秒),識別結果
30         cid, result = yundama.decode(filename, codetype, timeout)
31         print('cid: %s, result: %s' % (cid, result))
32         return result
parse_code_img

以後哪裏須要驗證碼處理將此兩段代碼複製到一個文件裏,調用此文件,執行parse_code_img方法便可獲取驗證碼的文本信息api

相關文章
相關標籤/搜索