Python爬蟲入門教程 55-100 python爬蟲高級技術之驗證碼篇


驗證碼探究

如果你是一個數據挖掘愛好者,那么驗證碼是你避免不過去的一個天坑,和各種驗證碼斗爭,必然是你成長的一條道路,接下來的幾篇文章,我會盡量的找到各種驗證碼,並且去嘗試解決掉它,中間有些技術甚至我都沒有見過,來吧,一起Coding吧

數字+字母的驗證碼

我隨便在百度圖片搜索了一個驗證碼,如下
驗證碼
今天要做的是驗證碼識別中最簡單的一種辦法,采用pytesseract解決,它屬於Python當中比較簡單的OCR識別庫

庫的安裝

使用pytesseract之前,你需要通過pip 安裝一下對應的模塊 ,需要兩個

pytesseract庫還有圖像處理的pillow庫了

pip install pytesseract
pip install pillow

如果你安裝了這兩個庫之后,編寫一個識別代碼,一般情況下會報下面這個錯誤

pytesseract.pytesseract.TesseractNotFoundError: tesseract is not installed or it's not in your path

這是由於你還缺少一部分內容

安裝一個Tesseract-OCR軟件。這個軟件是由Google維護的開源的OCR軟件。

下載地址 > https://github.com/tesseract-ocr/tesseract/wiki

中文包的下載地址 > https://github.com/tesseract-ocr/tessdata

選擇你需要的版本進行下載即可

pillow庫的基本操作

命令 釋義
open() 打開一個圖片
from PIL import Image
im = Image.open("1.png")
im.show()
save() 保存文件
convert() convert() 是圖像實例對象的一個方法,接受一個 mode 參數,用以指定一種色彩模式,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) 
# 邊緣增強 
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) 
# 細節 
im.filter(ImageFilter.DETAIL)

Format

format屬性定義了圖像的格式,如果圖像不是從文件打開的,那么該屬性值為None;
size屬性是一個tuple,表示圖像的寬和高(單位為像素);
mode屬性為表示圖像的模式,常用的模式為:L為灰度圖,RGB為真彩色,CMYK為pre-press圖像。如果文件不能打開,則拋出IOError異常。

這個地方可以參照一篇博客,寫的不錯 > https://www.cnblogs.com/mapu/p/8341108.html

驗證碼識別

注意安裝完畢,如果還是報錯,請找到模塊 pytesseract.py 這個文件,對這個文件進行編輯

一般這個文件在 C:\Program Files\Python36\Lib\site-packages\pytesseract\pytesseract.py 位置

文件中 tesseract_cmd = 'tesseract' 改為自己的地址
例如: tesseract_cmd = 'C:\Program Files (x86)\Tesseract-OCR\tesseract.exe' 

如果報下面的BUG,請注意

Error opening data file \Program Files (x86)\Tesseract-OCR\tessdata/chi_sim.traineddata Please make sure the TESSDATA_PREFIX environment variable

解決辦法也比較容易,按照它的提示,表示缺失了 TESSDATA_PREFIX 這個環境變量。你只需要在系統環境變量中添加一條即可

將 TESSDATA_PREFIX=C:\Program Files (x86)\Tesseract-OCR 添加環境變量

重啟IDE或者重新CMD,然后繼續運行代碼,這個地方注意需要用管理員運行你的py腳本

步驟分為

  1. 打開圖片 Image.open()
  2. pytesseract識別圖片
import pytesseract
from PIL import Image

def main():
    image = Image.open("1.jpg")
 
    text = pytesseract.image_to_string(image,lang="chi_sim")
    print(text)

if __name__ == '__main__':
    main()

測試英文,數字什么的基本沒有問題,中文簡直慘不忍睹。空白比較大的可以識別出來。唉~不好用
當然剛才那個7364 十分輕松的就識別出來了。

帶干擾的驗證碼識別

接下來識別如下的驗證碼,我們首先依舊先嘗試一下。運行代碼發現沒有任何顯示。接下來需要對這個圖片進行處理
在這里插入圖片描述
基本原理都是完全一樣的

  1. 彩色轉灰度
  2. 灰度轉二值
  3. 二值圖像識別

彩色轉灰度

im = im.convert('L')  

灰度轉二值,解決方案比較成套路,采用閾值分割法,threshold為分割點

def initTable(threshold=140):
    table = []
    for i in range(256):
        if i < threshold:
            table.append(0)
        else:
            table.append(1)
    return table

調用

binaryImage = im.point(initTable(), '1')
binaryImage.show()

調整之后
python驗證碼
我們還需要對干擾線進行處理。在往下研究去,是圖片深入處理的任務,對付小網站的簡單驗證碼,這個辦法足夠了,本篇博文OVER,下一篇我們繼續研究驗證碼。

參考鏈接

tesserocr GitHub:https://github.com/sirfz/tesserocr
tesserocr PyPI:https://pypi.python.org/pypi/tesserocr
pytesserocr GitHub:https://github.com/madmaze/pytesseract
pytesserocr PyPI:https://pypi.org/project/pytesseract/
tesseract下載地址:http://digi.bib.uni-mannheim.de/tesseract
tesseract GitHub:https://github.com/tesseract-ocr/tesseract
tesseract 語言包:https://github.com/tesseract-ocr/tessdata
tesseract文檔:https://github.com/tesseract-ocr/tesseract/wiki/Documentation

掃碼關注微信公眾賬號,領取2T學習資源


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM