操作系統(tǒng):windows 10
安裝模塊:Pillow?pytesseract?tesseract-ocr
1、首先安裝Pillow,可以選擇安裝windows版的安裝包
? ?下載地址:https://pypi.org/project/Pillow/#files
? ?此處我選擇安裝?Pillow-6.1.0.win-amd64-py2.7.exe
2、安裝完成后再進(jìn)行安裝??pytesseract?
? ?下載地址:https://pypi.org/project/pytesseract/#files
? ?解壓安裝包,然后切換到根目錄,執(zhí)行 : python setup.py install
3、接著繼續(xù)安裝?tesseract-ocr
? ?下載地址:https://download.csdn.net/download/wangleittt/10358361
? ? 安裝完成后,設(shè)置環(huán)境變量
? ??TESSDATA_PREFIX?= C:\Program Files (x86)\Tesseract-OCR
? ? 然后在Path 新增?C:\Program Files (x86)\Tesseract-OCR
以上即完成了環(huán)境的安裝,接下來(lái)就愉快的玩耍代碼吧!
附上我的代碼:
#!/usr/bin/python
#-*- coding:utf8 -*-
# encoding: utf-8
# auth by huwj 2019-07-29
from selenium import webdriver
import time
from PIL import Image, ImageEnhance
from pytesseract import image_to_string
URL = "http://www.xxxx.com"
user = 'hu_wen'
password = '123456'
def login():
try:
# 靜默模式
# option = webdriver.ChromeOptions()
# option.add_argument('headless')
# driver = webdriver.Chrome(chrome_options=option)
# 實(shí)例化出一個(gè)chrome瀏覽器
driver = webdriver.Chrome()
# 設(shè)置瀏覽器窗口的位置和大小
driver.maximize_window()
driver.get(URL)
time.sleep(2)
# 通過(guò)使用選擇器選擇到表單元素進(jìn)行模擬輸入和點(diǎn)擊按鈕提交
driver.find_element_by_id('txt_user').clear()
driver.find_element_by_id('txt_user').send_keys(user)
driver.find_element_by_id('txt_pwd').clear()
driver.find_element_by_id('txt_pwd').send_keys(password)
# 識(shí)別圖片驗(yàn)證碼
driver.save_screenshot('D://360Downloads/All.png') # 截取當(dāng)前網(wǎng)頁(yè),該網(wǎng)頁(yè)有我們需要的驗(yàn)證碼
imgelement = driver.find_element_by_id('verifyImg')
location = imgelement.location # 獲取驗(yàn)證碼x,y軸坐標(biāo)
size = imgelement.size # 獲取驗(yàn)證碼的長(zhǎng)寬
rangle = (int(location['x']), int(location['y']), int(location['x'] + size['width']),
int(location['y'] + size['height'])) # 寫(xiě)成我們需要截取的位置坐標(biāo)
i = Image.open("D://360Downloads/All.png") # 打開(kāi)截圖
result = i.crop(rangle) # 使用Image的crop函數(shù),從截圖中再次截取我們需要的區(qū)域
result.save('D://360Downloads/result.png')
text = image_to_string('D://360Downloads/result.png', 'eng').strip()
print("pic_text:", text)
driver.find_element_by_id("txt_verifyCode").send_keys(text)
driver.find_element_by_id('btn_login').click()
time.sleep(6)
return driver
except Exception, e:
print e
driver.quit()
?
?
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
