【python爬虫笔记】验证码
Index of /tesseract (uni-mannheim.de)
https://github.com/tesseract-ocr/tessdata
目錄
Python 驗(yàn)證碼
數(shù)字+字母的驗(yàn)證碼
pytesseract庫的安裝
pillow 庫
Filter
Format
帶干擾的驗(yàn)證碼識(shí)別
獲取 Access Token
通過百度模塊調(diào)用驗(yàn)證碼識(shí)別
Python 驗(yàn)證碼
如果你是一個(gè)數(shù)據(jù)挖掘愛好者,那么驗(yàn)證碼是你避免不過去的一個(gè)天坑,和各種驗(yàn)證碼斗爭,必然是你成長的一條道路,
本篇文章為你帶來驗(yàn)證碼的入門案例,來吧,一起 Coding 吧。
數(shù)字+字母的驗(yàn)證碼
我隨便在百度圖片搜索了一個(gè)驗(yàn)證碼,如下
今天要做的是驗(yàn)證碼識(shí)別中最簡單的一種辦法,采用pytesseract解決,它屬于 Python 當(dāng)中比較簡單的OCR識(shí)別庫
pytesseract庫的安裝
超詳細(xì)解決pytesseract.pytesseract.TesseractNotFoundError: tesseract is not installed or it's not in yo...
Tesseract OCR V5.0安裝教程(Windows
使用pytesseract之前,你需要通過 pip 安裝一下對(duì)應(yīng)的模塊 ,需要兩個(gè)
pytesseract 庫還有圖像處理的 pillow 庫了
pip install pytesseract pip install pillow如果你安裝了這兩個(gè)庫之后,編寫一個(gè)識(shí)別代碼,一般情況下會(huì)報(bào)下面這個(gè)錯(cuò)誤
pytesseract.pytesseract.TesseractNotFoundError: tesseract is not installed or it's not in your path這是由于你還缺少一部分內(nèi)容
安裝一個(gè) Tesseract-OCR 軟件。這個(gè)軟件是由 Google 維護(hù)的開源的 OCR 軟件。
下載地址 > https://github.com/tesseract-ocr/tesseract/wiki
中文包的下載地址 > https://github.com/tesseract-ocr/tessdata
選擇你需要的版本進(jìn)行下載即可
pillow 庫
| open() | 打開一個(gè)圖片 from PIL import Image im = Image.open(“1.png”) im.show() |
| save() | 保存文件 |
| convert() | convert() 是圖像實(shí)例對(duì)象的一個(gè)方法,接受一個(gè) mode 參數(shù),用以指定一種色彩模式,mode 的取值可以是如下幾種: · 1 (1-bit pixels, black and white, stored with one pixel per byte) · L (8-bit pixels, black and white) · P (8-bit pixels, mapped to any other mode using a colour palette) · RGB (3x8-bit pixels, true colour) · RGBA (4x8-bit pixels, true colour with transparency mask) · CMYK (4x8-bit pixels, colour separation) · YCbCr (3x8-bit pixels, colour video format) · I (32-bit signed integer pixels) · F (32-bit floating point pixels) |
Filter
from PIL import Image, ImageFilter im = Image.open(‘1.png’) # 高斯模糊 im.filter(ImageFilter.GaussianBlur) # 普通模糊 im.filter(ImageFilter.BLUR) # 邊緣增強(qiáng) im.filter(ImageFilter.EDGE_ENHANCE) # 找到邊緣 im.filter(ImageFilter.FIND_EDGES) # 浮雕 im.filter(ImageFilter.EMBOSS) # 輪廓 im.filter(ImageFilter.CONTOUR) # 銳化 im.filter(ImageFilter.SHARPEN) # 平滑 im.filter(ImageFilter.SMOOTH) # 細(xì)節(jié) im.filter(ImageFilter.DETAIL)Format
format 屬性定義了圖像的格式,如果圖像不是從文件打開的,那么該屬性值為 None;
size 屬性是一個(gè) tuple,表示圖像的寬和高(單位為像素);
mode 屬性為表示圖像的模式,常用的模式為:L 為灰度圖,RGB 為真彩色,CMYK 為 pre-press 圖像。如果文件不能打開,則拋出 IOError 異常。
驗(yàn)證碼識(shí)別
注意安裝完畢,如果還是報(bào)錯(cuò),請(qǐng)找到模塊 pytesseract.py 這個(gè)文件,對(duì)這個(gè)文件進(jìn)行編輯
一般這個(gè)文件在?C:\Program Files\Python36\Lib\site-packages\pytesseract\pytesseract.py?位置
文件中 tesseract_cmd = 'tesseract' 改為自己的地址 例如: tesseract_cmd = 'C:\Program Files (x86)\Tesseract-OCR\tesseract.exe'如果報(bào)下面的 BUG,請(qǐng)注意
Error opening data file \Program Files (x86)\Tesseract-OCR\tessdata/chi_sim.traineddata Please make sure the TESSDATA_PREFIX environment variable
解決辦法也比較容易,按照它的提示,表示缺失了 TESSDATA_PREFIX 這個(gè)環(huán)境變量。你只需要在系統(tǒng)環(huán)境變量中添加一條即可
將 TESSDATA_PREFIX=C:\Program Files (x86)\Tesseract-OCR 添加環(huán)境變量
重啟 IDE 或者重新 CMD,然后繼續(xù)運(yùn)行代碼,這個(gè)地方注意需要用管理員運(yùn)行你的 py 腳本
步驟分為
測試英文,數(shù)字什么的基本沒有問題,中文簡直慘不忍睹。空白比較大的可以識(shí)別出來。唉~不好用
當(dāng)然剛才那個(gè)7364?十分輕松的就識(shí)別出來了。
帶干擾的驗(yàn)證碼識(shí)別
接下來識(shí)別如下的驗(yàn)證碼,我們首先依舊先嘗試一下。運(yùn)行代碼發(fā)現(xiàn)沒有任何顯示。接下來需要對(duì)這個(gè)圖片進(jìn)行處理
基本原理都是完全一樣的
彩色轉(zhuǎn)灰度
im = im.convert('L')灰度轉(zhuǎn)二值,解決方案比較成套路,采用閾值分割法,threshold 為分割點(diǎn)
def initTable(threshold=140):table = []for i in range(256):if i < threshold:table.append(0)else:table.append(1)return table調(diào)用
binaryImage = im.point(initTable(), '1')#像素遍歷 binaryImage.show()調(diào)整之后
通過百度提供的接口實(shí)現(xiàn)驗(yàn)證碼識(shí)別
今天你要學(xué)習(xí)的驗(yàn)證碼采用通過第三方 AI 平臺(tái)開放的 OCR 接口實(shí)現(xiàn),OCR 文字識(shí)別技術(shù)目前已經(jīng)比較成熟了,而且第三方比較多,今天采用的是百度的。
獲取 Access Token
程序調(diào)用接口是要生成access_token
Access Token 用于基于 Token 的認(rèn)證模式,允許應(yīng)用訪問一個(gè)資源 API。
- ?先去創(chuàng)建一個(gè)應(yīng)用
?百度智能云-管理中心 (baidu.com)
- 請(qǐng)求URL數(shù)據(jù)格式
向授權(quán)服務(wù)地址https://aip.baidubce.com/oauth/2.0/token發(fā)送請(qǐng)求(推薦使用POST),并在URL中帶上以下參數(shù):
- grant_type:?必須參數(shù),固定為client_credentials;
- client_id:?必須參數(shù),應(yīng)用的API Key;
- client_secret:?必須參數(shù),應(yīng)用的Secret Key;
注:
- API Key、Secret Key?均可在百度智能云控制臺(tái)?各技術(shù)方向概覽頁的應(yīng)用列表?處獲取,若無應(yīng)用請(qǐng)先進(jìn)行創(chuàng)建;
- API Key、Secret Key用于接口調(diào)用鑒權(quán),請(qǐng)務(wù)必注意保密,不可在公開文檔或代碼中以明文展示,否則可能導(dǎo)致賬號(hào)被盜用。
?百度官網(wǎng)獲取access_token示例代碼
# encoding:utf-8 import requests # client_id 為官網(wǎng)獲取的API Key, client_secret 為官網(wǎng)獲取的Secret Key host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=【官網(wǎng)獲取的AK】&client_secret=【官網(wǎng)獲取的SK】' response = requests.get(host) if response:print(response.json())請(qǐng)求百度orc通用文字識(shí)別API
有了access_token之后就可以開始識(shí)別請(qǐng)求了。
HTTP 方法:POST
請(qǐng)求URL: https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic
請(qǐng)求頭: Content-Type application/x-www-form-urlencoded
請(qǐng)求參數(shù):
- image : 圖像數(shù)據(jù),base64編碼,要求base64編碼后大小不超過4M,最短邊至少15px,最長邊最大4096px,支持jpg/png/bmp格式,當(dāng)image字段存在時(shí)url字段失效
- url : 圖片完整URL,URL長度不超過1024字節(jié),URL對(duì)應(yīng)的圖片base64編碼后大小不超過4M,最短邊至少15px,最長邊最大4096px,支持jpg/png/bmp格式,當(dāng)image字段存在時(shí)url字段失效
?{'words_result': [{'words': '已只成此'}, {'words': '是生月蝶柱錦'}, {'words': '當(dāng)煙明望思瑟'}, {'words': '時(shí)此珠帝無'}, {'words': '惘是追情'}, {'words': '已情有春年端'}, {'words': '惘可心莊五'}, {'words': '然當(dāng)憶可'}, {'words': '然待藍(lán)托生十'}, {'words': '成杜曉絃'}, {'words': '追日夢'}, {'words': '時(shí)待'}, {'words': '憶暖迷絃'}, {'words': '只玉海蝴'}], 'words_result_num': 14, 'direction': 0, 'log_id': 1592394631485448641}
脫離百度提供的文檔自己編寫:
獲得access_token
def get_accesstoken(self):res = requests.post(self.url.format(self.key,self.secret),headers=self.header)content = res.textif (content):return json.loads(content)["access_token"] import requests import jsonimport base64import urllib.request, urllib.parseclass GetCode(object):def __init__(self):self.url = "https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id={}&client_secret={}"self.api = "https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic?access_token={}"self.header = {"Content-Type":'application/json; charset=UTF-8'}self.key = "你的KEY"self.secret = "你的SECRET"?驗(yàn)證碼識(shí)別階段
普通沒有干擾的驗(yàn)證碼,我們直接識(shí)別即可,但是有的驗(yàn)證碼還是有干擾的,在識(shí)別之前,需要對(duì)它進(jìn)行基本的處理,我們采用和上篇文章類似的辦法進(jìn)行,對(duì)它進(jìn)行灰度處理和二值化操作。
def init_table(self,threshold=155):table = []for i in range(256):if i < threshold:table.append(0)else:table.append(1)return tabledef opt_image(self):im = Image.open("OIP-C.jpg")im = im.convert('L')im = im.point(self.init_table(), '1')#遍歷每一個(gè)像素點(diǎn)im.save('OIP-C.jpg_s.png')return "OIP-C.jpg_s.png"調(diào)用驗(yàn)證碼接口
調(diào)用百度的驗(yàn)證碼接口,不使用百度給的模塊,直接編寫。按照它對(duì)應(yīng)的文檔書寫
def get_file_content(self,file_path):with open(file_path, 'rb') as fp:base64_data = base64.b64encode(fp.read())s = base64_data.decode()data = {}data['image'] = sdecoded_data = urllib.parse.urlencode(data)return decoded_datadef show_code(self):image = self.get_file_content(self.opt_image())headers = {"Content-Type": "application/x-www-form-urlencoded"}res = requests.post(self.api.format(self.get_accesstoken()),headers=headers,data=image)print(res.text)?通過百度模塊調(diào)用驗(yàn)證碼識(shí)別
安裝百度 AI
pip install baidu-aip??
from aip import AipOcr #baidu_aip包中的AipOCR實(shí)現(xiàn)圖像文字識(shí)別# 定義常量 APP_ID = '15736693' API_KEY = '你的KEY' SECRET_KEY = '你的SECRET'# 初始化文字識(shí)別 aipOcr=AipOcr(APP_ID, API_KEY, SECRET_KEY)# 讀取圖片 filePath = "OIP-C.jpg"def get_file_content(filePath):with open(filePath, 'rb') as fp:return fp.read()# 定義參數(shù)變量 options = {'detect_direction': 'true','language_type': 'CHN_ENG', }# 網(wǎng)絡(luò)圖片文字文字識(shí)別接口 result = aipOcr.webImage(get_file_content(filePath),options)print(result) {'words_result': [{'words': '已只成此'}, {'words': '是生月蝶柱錦'}, {'words': '當(dāng)煙明望思瑟'}, {'words': '時(shí)此珠帝無'}, {'words': '惘是追情'}, {'words': '已情有春年端'}, {'words': '惘可心莊五'}, {'words': '然當(dāng)憶可'}, {'words': '然待藍(lán)托生十'}, {'words': '成杜曉絃'}, {'words': '追日夢'}, {'words': '時(shí)待'}, {'words': '憶暖迷絃'}, {'words': '只玉海蝴'}], 'words_result_num': 14, 'direction': 0, 'log_id': 1592394631485448641}文字識(shí)別OCR (baidu.com)
總結(jié)
以上是生活随笔為你收集整理的【python爬虫笔记】验证码的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 零基础带你Ubuntu20.04安装Ro
- 下一篇: android白色暗色主题,使用Kotl