2019.8.5
- selenium(讀作:舍利尼恩)的核心Selenium Core基于JsUnit,完全由JavaScript編寫,因此可以用于任何支持JavaScript的瀏覽器上。selenium可以模擬真實瀏覽器,自動化測試工具,支持多種瀏覽器,爬蟲中主要用來解決JavaScript渲染問題。
- selenium需要下載瀏覽器驅(qū)動,這是世界第一瀏覽器chrome driver 淘寶鏡像。
- PhantomJS是無頭瀏覽器無須打開瀏覽器就可以進行爬蟲,但在未來的通知之前,PhantomJS 2.1.1將會是已知最后的穩(wěn)定版本,其已停止開發(fā),新版本的Selenium不再支持PhantomJS,但可以使用Chrome或Firefox的無頭版本來替代,這兩個瀏覽器的無頭模式使得PhamtomJS的優(yōu)勢喪失了,所以我們應(yīng)該轉(zhuǎn)用無頭模式。
-
pytesseract是一個基于google's Tesseract-OCR的API封裝包,pytesseract默認支持tiff、bmp格式圖片,只有在安裝PIL庫之后,才能支持jpeg、gif、png等其他圖片格式。要將tesseract添加進環(huán)境變量內(nèi)。 - 一般圖像處理驗證,需要通過對圖像進行灰度處理、二值化后增加圖像文字的辨識度。
# 處理圖片
def bin(image, threshold):
image = image.convert('L')
table = []
for i in range(256):
if i < threshold:
table.append(0)
else:
table.append(1)
return image.point(table,'1')
- 比較處理前后的識別準(zhǔn)確率。圖片地址1.jpg
from PIL import Image
import pytesseract
# tesseract不能直接對網(wǎng)頁驗證碼識別,需要處理,這里用圖片
img = Image.open('1.jpg')
result = pytesseract.image_to_string(img, lang='chi_sim') # 默認識別英文和數(shù)字,識別簡體中文要改參數(shù)
print(result)
# 處理圖片
def bin(image, threshold):
image = image.convert('L')
table = []
for i in range(256):
if i < threshold:
table.append(0)
else:
table.append(1)
return image.point(table,'1')
image = bin(img, 127)
image.show()
consequence = pytesseract.image_to_string(image, lang='chi_sim') # 輸出為字符串,還有其他函數(shù)輸出更多內(nèi)容
print(consequence)
result = 'www zhIMA365.coM'
consequence = '町 知 碼 網(wǎng)'
- 參考文章:Python之selenium+pytesseract實現(xiàn)識別驗證碼自動化登陸腳本——卿先生 https://www.cnblogs.com/-qing-/p/11027821.html
