20190125補充,換了個新電腦win10,安裝都很正常,但是在匯入模組的時候出錯,提示如下:
traceback (most recent call last):
file 「」, line 1, in
import tesserocr
file 「d:\program files\python\lib\site-packages\tesserocr_init_.py」, line 1, in
from ._tesserocr import *
importerror: dll load failed: 找不到指定的模組。
最後感覺是依賴庫對最新的testesserocr 2.4-3.7 不支援,就降級了乙個版本,之後解決了問題
tesserocr-2.3.1-cp37-cp37m-win_amd64.whl 這個是降級後的
tesserocr-2.4.0-cp37-cp37m-win_amd64.whl 這個是之前正常安裝,呼叫出錯的
介紹在爬蟲過程中,難免會遇到各種各樣的驗證碼,而大多數驗證碼還是圖形驗證碼,這時候我們可以直接用 ocr 來識別。
tesserocr 是 python 的乙個 ocr 識別庫 ,但其實是對 tesseract 做的一 層 python api 封裝,所以它的核心是 tesseract。 因此,在安裝 tesserocr 之前,我們需要先安裝 tesseract 。
例如:對於下圖的驗證碼,我們可以通過 ocr 技術將其轉換成電子文字,然後爬蟲將識別的結果提交給伺服器,便可以達到自動識別驗證碼的過程。
相關鏈結
tesserocr github:
tesserocr pypi:
tesseract github:
tesseract 語言包:
tesseract 文件: /wiki/documentation
給tesseract配置環境變數:
(1)將tesseract安裝路徑新增到path環境變數中
(2)將tesseract的語言包新增到環境變數中,在環境變數中新建乙個系統變數,變數名稱為tessdata_prefix,tessdata是放置語言包的資料夾,一般在你安裝tesseract的目錄下,即tesseract的安裝目錄就是tessdata的父目錄,把tessdata_prefix的值設定為它即可
接下來 , 再安裝 tesserocr 即可:
pip install tesserocr pillow
如果通過 pip 安裝失敗,可以嘗試 anaconda 下的 conda 來安裝:
conda install -c simonflueckiger tesserocr pillow
驗證安裝
測試樣例:
(1)用 tesseract 命令測試:
tesseract image.png result -l eng
執行結果如下:
tesseract open source ocr engine v3.05.01 with leptonica
(2)利用 python **測試:
import tesserocr
from pil import image
image = image.open(『image.png』)
result = tesserocr.image_to_text(image)
print(result)
執行結果如下:
pythonwebspider
另外,還可以直接呼叫 tesserocr 模組的 file_to_text() 方法,可以達到同樣的效果:
import tesserocr
print(tesserocr.file_to_text(『image.png』))
執行結果如下:
pythonwebspider
如果成功輸出結果,則證明 tesseract 和 tesserocr 都已經安裝成功。
第三方模組
參考 第三方模組 requestsimport requests 對於帶引數的url,傳入乙個dict作為params引數 params 要傳入http header時,我們傳入乙個dict作為headers引數 請求中傳入cookie,只需準備乙個dict傳入cookies引數 my cookie...
python模組之第三方模組
什麼是第三方模組 1 命令列借助於pip工具 pip3 install 模組名 不知道版本預設是最新版 pip3 install 模組名 版本號 pip3 install 模組名 i 倉庫位址 臨時切換 注 pip所在的路徑需要新增環境變數 命令列形式永久修改需要修改python直譯器原始檔 例 p...
python 第三方模組(itchat)
import itchat import time import random itchat.auto login 登陸 while true itchat.send hello tousername filehelper time.sleep random.randint 1 3 隨機隔1 3秒i...