操作系統: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、安裝完成后再進行安裝??pytesseract?
? ?下載地址:https://pypi.org/project/pytesseract/#files
? ?解壓安裝包,然后切換到根目錄,執行 : python setup.py install
3、接著繼續安裝?tesseract-ocr
? ?下載地址:https://download.csdn.net/download/wangleittt/10358361
? ? 安裝完成后,設置環境變量
? ??TESSDATA_PREFIX?= C:\Program Files (x86)\Tesseract-OCR
? ? 然后在Path 新增?C:\Program Files (x86)\Tesseract-OCR
以上即完成了環境的安裝,接下來就愉快的玩耍代碼吧!
附上我的代碼:
#!/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)
# 實例化出一個chrome瀏覽器
driver = webdriver.Chrome()
# 設置瀏覽器窗口的位置和大小
driver.maximize_window()
driver.get(URL)
time.sleep(2)
# 通過使用選擇器選擇到表單元素進行模擬輸入和點擊按鈕提交
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)
# 識別圖片驗證碼
driver.save_screenshot('D://360Downloads/All.png') # 截取當前網頁,該網頁有我們需要的驗證碼
imgelement = driver.find_element_by_id('verifyImg')
location = imgelement.location # 獲取驗證碼x,y軸坐標
size = imgelement.size # 獲取驗證碼的長寬
rangle = (int(location['x']), int(location['y']), int(location['x'] + size['width']),
int(location['y'] + size['height'])) # 寫成我們需要截取的位置坐標
i = Image.open("D://360Downloads/All.png") # 打開截圖
result = i.crop(rangle) # 使用Image的crop函數,從截圖中再次截取我們需要的區域
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()
?
?
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

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