Python爬蟲之超級鷹驗證碼應(yīng)用
超級鷹平臺
驗證碼的破解可以有以下方式:
- 簡單的數(shù)字字母組合可以使用圖像識別(python 現(xiàn)成模塊),成功率不高
- 使用第三方打碼平臺(破解驗證碼平臺),花錢,把驗證碼圖片給它,返回識別完的結(jié)果
第三方平臺有超級鷹等等。
基礎(chǔ)使用
在其官網(wǎng)注冊賬號后,綁定微信會提供免費的1000題分,可用于驗證碼識別
創(chuàng)建開發(fā)者賬號,并且注冊一個軟件

下載 python demo

基礎(chǔ)使用
下載的demo是使用python2編寫的,需要簡單修改
import requests
from hashlib import md5
class ChaojiyingClient(object):
def __init__(self, username, password, soft_id):
self.username = username
password = password.encode('utf8')
self.password = md5(password).hexdigest()
self.soft_id = soft_id
self.base_params = {
'user': self.username,
'pass2': self.password,
'softid': self.soft_id,
}
self.headers = {
'Connection': 'Keep-Alive',
'User-Agent': 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)',
}
def PostPic(self, im, codetype):
"""
im: 圖片字節(jié)
codetype: 題目類型 參考 http://www.chaojiying.com/price.html
"""
params = {
'codetype': codetype,
}
params.update(self.base_params)
files = {'userfile': ('ccc.jpg', im)}
r = requests.post('http://upload.chaojiying.net/Upload/Processing.php', data=params, files=files,
headers=self.headers)
return r.json()
def PostPic_base64(self, base64_str, codetype):
"""
im: 圖片字節(jié)
codetype: 題目類型 參考 http://www.chaojiying.com/price.html
"""
params = {
'codetype': codetype,
'file_base64': base64_str
}
params.update(self.base_params)
r = requests.post('http://upload.chaojiying.net/Upload/Processing.php', data=params, headers=self.headers)
return r.json()
def ReportError(self, im_id):
"""
im_id:報錯題目的圖片ID
"""
params = {
'id': im_id,
}
params.update(self.base_params)
r = requests.post('http://upload.chaojiying.net/Upload/ReportError.php', data=params, headers=self.headers)
return r.json()
if __name__ == '__main__':
chaojiying = ChaojiyingClient('超級鷹用戶名', '超級鷹用戶名的密碼', '96001') # 用戶中心>>軟件ID 生成一個替換 96001
im = open('a.jpg', 'rb').read() # 本地圖片文件路徑 來替換 a.jpg 有時WIN系統(tǒng)須要//
print(chaojiying.PostPic(im, 1902)) # 1902 驗證碼類型 官方網(wǎng)站>>價格體系 3.4+版 print 后要加()
# print chaojiying.PostPic(base64_str, 1902) #此處為傳入 base64代碼
剪切驗證碼
實際使用的時候驗證碼是不固定的,需要剪切下來使用,需要使用 pillow 模塊
截圖需要注意分辨率
from selenium import webdriver
from selenium.webdriver.common.by import By
from PIL import Image
from selenium.webdriver.chrome.options import Options
from chaojiying import chaojiying_Python
chrome_options = Options()
chrome_options.add_argument('window-size=1920x1080') # 指定瀏覽器分辨率
chrome_options.add_argument('--disable-gpu') # 谷歌文檔提到需要加上這個屬性來規(guī)避bug
chrome_options.add_argument('--hide-scrollbars') # 隱藏滾動條, 應(yīng)對一些特殊頁面
# chrome_options.add_argument('blink-settings=imagesEnabled=false') # 不加載圖片, 提升速度
chrome_options.add_argument('--headless') # 瀏覽器不提供可視化頁面. linux下如果系統(tǒng)不支持可視化不加這條會啟動失敗
# chrome = webdriver.Chrome(executable_path='../chromedriver.exe')
chrome = webdriver.Chrome(executable_path='../chromedriver.exe', options=chrome_options)
chrome.implicitly_wait(10)
chrome.maximize_window()
try:
chrome.get('http://www.aa7a.cn/user.php?')
username = chrome.find_element(By.ID, 'username')
password = chrome.find_element(By.ID, 'password')
captcha = chrome.find_element(By.ID, 'captcha')
# 保存大圖
chrome.save_screenshot('main.png')
img = chrome.find_element(By.ID, 'login_img_checkcode')
img_location = img.location
img_size = img.size
# 使用pillow扣除大圖中的驗證碼
img_tu = (
int(img_location['x']),
int(img_location['y']),
int(img_location['x'] + img_size['width']),
int(img_location['y'] + img_size['height']),
)
# 打開頁面大圖
im = Image.open('./main.png')
# 剪切驗證碼圖片
fram = im.crop(img_tu)
# 保存驗證碼圖片
fram.save('code.png')
# 打開驗證碼圖片
code_img = open('code.png', 'rb').read()
# 調(diào)用超級鷹識別
res = chaojiying_Python.chaojiying.PostPic(code_img, 1902)
code = res.get('pic_str')
username.send_keys('username')
password.send_keys('123')
captcha.send_keys(code)
print(code)
except Exception as e:
print(e)
finally:
chrome.quit()
到此這篇關(guān)于Python爬蟲之超級鷹驗證碼應(yīng)用的文章就介紹到這了,更多相關(guān)Python驗證碼內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python實現(xiàn)遍歷windows所有窗口并輸出窗口標題的方法
這篇文章主要介紹了Python實現(xiàn)遍歷windows所有窗口并輸出窗口標題的方法,涉及Python調(diào)用及遍歷windows窗口句柄的技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-03-03
Pycharm?cannot?set?up?a?python?SDK問題的原因及解決方法
這篇文章主要給大家介紹了關(guān)于Pycharm?cannot?set?up?a?python?SDK問題的原因及解決方法,這個問題已經(jīng)不是第一次出現(xiàn)了,所以干脆總結(jié)下,需要的朋友可以參考下2022-06-06

