驗證碼處理

目錄html

驗證碼處理

雲打碼平臺處理驗證碼的實現流程:json

  • 1.對攜帶驗證碼的頁面數據進行抓取
  • 2.能夠將頁面數據中驗證碼進行解析,驗證碼圖片下載到本地
  • 3.能夠將驗證碼圖片提交給三方平臺進行識別,返回驗證碼圖片上的數據值
    • 雲打碼平臺:
      • 1.在官網中進行註冊(普通用戶和開發者用戶)
      • 2.登陸開發者用戶:
        • 1.實例代碼的下載(開發文檔-》調用實例及最新的DLL-》PythonHTTP實例下載)
        • 2.建立一個軟件:個人軟件-》添加新的軟件
          -3.使用示例代碼中的源碼文件中的代碼進行修改,讓其識別驗證碼圖片中的數據值

代碼展現:app

#該函數就調用了打碼平臺的相關的接口對指定的驗證碼圖片進行識別,返回圖片上的數據值
def getCode(codeImg):
    # 雲打碼平臺普通用戶的用戶名
    username    = 'bobo328410948'

    # 雲打碼平臺普通用戶的密碼
    password    = 'bobo328410948'                            
    
    # 軟件ID,開發者分紅必要參數。登陸開發者後臺【個人軟件】得到!
    appid       = 6003                                     

    # 軟件密鑰,開發者分紅必要參數。登陸開發者後臺【個人軟件】得到!
    appkey      = '1f4b564483ae5c907a1d34f8e2f2776c'    

    # 驗證碼圖片文件
    filename    = codeImg                        

    # 驗證碼類型,# 例:1004表示4位字母數字,不一樣類型收費不一樣。請準確填寫,不然影響識別率。在此查詢全部類型 http://www.yundama.com/price.html
    codetype    = 3000

    # 超時時間,秒
    timeout     = 20                                    

    # 檢查
    if (username == 'username'):
        print('請設置好相關參數再測試')
    else:
        # 初始化
        yundama = YDMHttp(username, password, appid, appkey)

        # 登錄雲打碼
        uid = yundama.login();
        print('uid: %s' % uid)

        # 查詢餘額
        balance = yundama.balance();
        print('balance: %s' % balance)

        # 開始識別,圖片路徑,驗證碼類型ID,超時時間(秒),識別結果
        cid, result = yundama.decode(filename, codetype, timeout);
        print('cid: %s, result: %s' % (cid, result))
        
        return result

.函數

import requests
from lxml import etree
import json
import time
import re
#1.對攜帶驗證碼的頁面數據進行抓取
url = 'https://www.douban.com/accounts/login?source=movie'
headers = {
    'User-Agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Mobile Safari/537.36'
}
page_text = requests.get(url=url,headers=headers).text

#2.能夠將頁面數據中驗證碼進行解析,驗證碼圖片下載到本地
tree = etree.HTML(page_text)
codeImg_url = tree.xpath('//*[@id="captcha_image"]/@src')[0]
#獲取了驗證碼圖片對應的二進制數據值
code_img = requests.get(url=codeImg_url,headers=headers).content


#獲取capture_id
'<img id="captcha_image" src="https://www.douban.com/misc/captcha?id=AdC4WXGyiRuVJrP9q15mqIrt:en&amp;size=s" alt="captcha" class="captcha_image">'
c_id = re.findall('<img id="captcha_image".*?id=(.*?)&amp.*?>',page_text,re.S)[0]
with open('./code.png','wb') as fp:
    fp.write(code_img)

#得到了驗證碼圖片上面的數據值
codeText = getCode('./code.png')  
print(codeText)
#進行登陸操做
post = 'https://accounts.douban.com/login'
data = {
    "source": "movie",
    "redir": "https://movie.douban.com/",
    "form_email": "15027900535",
    "form_password": "bobo@15027900535",
    "captcha-solution":codeText,
    "captcha-id":c_id,
    "login": "登陸",
}
print(c_id)
login_text = requests.post(url=post,data=data,headers=headers).text
with open('./login.html','w',encoding='utf-8') as fp:
    fp.write(login_text)
相關文章
相關標籤/搜索