python爬虫验证码的处理(云打码)_python爬虫验证码的处理(云打码)
爬蟲開發(fā)過程中經(jīng)常會遇到驗證碼,對于普通的非滑塊驗證碼,首先要想辦法把驗證碼圖片保存到本地:用抓包工具抓包查看驗證碼圖片的url,通過這個url把圖片下載寫到二進制文件。接下來對驗證碼的處理方法有三種:
手動輸入驗證碼
接入打碼平臺
更換ip繞過驗證碼
先簡要介紹一下如何接入打碼平臺實現(xiàn)自動打碼。我選擇的是云打碼:http://www.yundama.com。注冊成為開發(fā)者,找客服可以要到調(diào)試分用來調(diào)試,查看開發(fā)文檔。
下載Python調(diào)用示例,文檔結(jié)構(gòu)如下:
打開YDMPython3.x.py查看源碼(Python2調(diào)用示例對應(yīng)的是YDMPython2.x.py):
# -*- coding: cp936 -*-
import sys
import os
from ctypes import *
# 下載接口放目錄 http://www.yundama.com/apidoc/YDM_SDK.html
# 錯誤代碼請查詢 http://www.yundama.com/apidoc/YDM_ErrorCode.html
# 所有函數(shù)請查詢 http://www.yundama.com/apidoc
print('>>>正在初始化...')
YDMApi = windll.LoadLibrary('yundamaAPI')
# 1. http://www.yundama.com/index/reg/developer 注冊開發(fā)者賬號
# 2. http://www.yundama.com/developer/myapp 添加新軟件
# 3. 使用添加的軟件ID和密鑰進行開發(fā),享受豐厚分成
appId = 1 # 軟件ID,開發(fā)者分成必要參數(shù)。登錄開發(fā)者后臺【我的軟件】獲得!
appKey = b'22cc5376925e9387a23cf797cb9ba745' # 軟件密鑰,開發(fā)者分成必要參數(shù)。登錄開發(fā)者后臺【我的軟件】獲得!
print('軟件ID:%d\r\n軟件密鑰:%s' % (appId, appKey))
# 注意這里是普通會員賬號,不是開發(fā)者賬號,注冊地址 http://www.yundama.com/index/reg/user
# 開發(fā)者可以聯(lián)系客服領(lǐng)取免費調(diào)試題分
username = b'test'
password = b'test'
if username == b'test':
exit('\r\n>>>請先設(shè)置用戶名密碼')
####################### 一鍵識別函數(shù) YDM_EasyDecodeByPath #######################
print('\r\n>>>正在一鍵識別...')
# 例:1004表示4位字母數(shù)字,不同類型收費不同。請準確填寫,否則影響識別率。在此查詢所有類型 http://www.yundama.com/price.html
codetype = 1004
# 分配30個字節(jié)存放識別結(jié)果
result = c_char_p(b" ")
# 識別超時時間 單位:秒
timeout = 60
# 驗證碼文件路徑
filename = b'getimage.jpg'
# 一鍵識別函數(shù),無需調(diào)用 YDM_SetAppInfo 和 YDM_Login,適合腳本調(diào)用
captchaId = YDMApi.YDM_EasyDecodeByPath(username, password, appId, appKey, filename, codetype, timeout, result)
print("一鍵識別:驗證碼ID:%d,識別結(jié)果:%s" % (captchaId, result.value))
################################################################################
########################## 普通識別函數(shù) YDM_DecodeByPath #########################
print('\r\n>>>正在登陸...')
# 第一步:初始化云打碼,只需調(diào)用一次即可
YDMApi.YDM_SetAppInfo(appId, appKey)
# 第二步:登陸云打碼賬號,只需調(diào)用一次即可
uid = YDMApi.YDM_Login(username, password)
if uid > 0:
print('>>>正在獲取余額...')
# 查詢賬號余額,按需要調(diào)用
balance = YDMApi.YDM_GetBalance(username, password)
print('登陸成功,用戶名:%s,剩余題分:%d' % (username, balance))
print('\r\n>>>正在普通識別...')
# 第三步:開始識別
# 例:1004表示4位字母數(shù)字,不同類型收費不同。請準確填寫,否則影響識別率。在此查詢所有類型 http://www.yundama.com/price.html
codetype = 1004
# 分配30個字節(jié)存放識別結(jié)果
result = c_char_p(b" ")
# 驗證碼文件路徑
filename = b'getimage.jpg'
# 普通識別函數(shù),需先調(diào)用 YDM_SetAppInfo 和 YDM_Login 初始化
captchaId = YDMApi.YDM_DecodeByPath(filename, codetype, result)
print("普通識別:驗證碼ID:%d,識別結(jié)果:%s" % (captchaId, result.value))
else:
print('登陸失敗,錯誤代碼:%d' % uid)
################################################################################
print('\r\n>>>錯誤代碼請查詢 http://www.yundama.com/apidoc/YDM_ErrorCode.html')
input('\r\n測試完成,按回車鍵結(jié)束...')
調(diào)用方法很簡單,先加載dll庫,然后調(diào)用一鍵識別函數(shù)即可,這段代碼看起來很雜亂,我封裝成函數(shù)以便調(diào)用,下面是我封裝好的函數(shù),刪除了源碼中普通識別函數(shù)的調(diào)用:
from ctypes import *
def ydm_func(ydm_appid, ydm_appkey, ydm_username, ydm_password, filename, captcha_type):
"""
封裝的云打碼函數(shù)
"""
YDMApi = windll.LoadLibrary('yundamaAPI-x64')
# 查詢所有類型 http://www.yundama.com/price.html
# 分配30個字節(jié)存放識別結(jié)果
result = c_char_p(b" ")
timeout = 10
captchaId = YDMApi.YDM_EasyDecodeByPath(ydm_username, ydm_password, ydm_appid, ydm_appkey, filename, captcha_type,
timeout, result)
print("一鍵識別:驗證碼ID:%d,識別結(jié)果:%s" % (captchaId, result.value))
return result.value
我的系統(tǒng)是64位的,故選擇加載yundamaAPI-x64.dll這個庫,識別完成返回驗證碼字符串。
使用示例如下,YDM_APPID,YDM_APPKEY,YDM_USERNAME,YDM_PASSWROD可以寫入配置文件,方便管理。
qr = ydm_func(YDM_APPID, YDM_APPKEY, YDM_USERNAME, YDM_PASSWORD, b'captcha.png',1004)
總結(jié)
以上是生活随笔為你收集整理的python爬虫验证码的处理(云打码)_python爬虫验证码的处理(云打码)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 计算机科学与技术考研课程安排,计算机科学
- 下一篇: 因子分析(SPSS)