前期安裝:
1、python3
2蠢沿、Pillow
3荧止、pytesseract
pip/pip3 install pytesseract
4、tesseract-ocr 下載地址
前期配置:
- 添加環(huán)境變量: TESSDATA_PREFIX = C:\Program Files (x86)\Tesseract-OCR
-
編輯文件 D:\Python35\Lib\site-packages\pytesseract\pytesseract.py骤星,
查找方式:在腳本中import pytesseract
將 tesseract_cmd = 'tesseract' =>
改為 tesseract_cmd = 'C:/Program Files (x86)/Tesseract-OCR/tesseract'
驗(yàn)證tesseract:
輸入命令:tesseract screenImg.png screenImg -l eng
官方資料
解決使用過程中的bug
selenium腳本
#!/usr/local/bin/python3
# -*- coding: UTF-8 -*-
__author__ = 'Alina'
from selenium import webdriver
import time,os
import pytesseract
from PIL import Image,ImageEnhance
from selenium.webdriver.support.ui import WebDriverWait
from pathlib import Path
usernameValue = "tester"
# 驗(yàn)證碼圖片保存地址
screenImg1 = r"/Users/Alina/Documents/shopXOTest/screenImg1.png"
screenImg2 = r"/Users/Alina/Documents/shopXOTest/screenImg2.png"
# tesseract存放位置
pytesseract.pytesseract.tesseract_cmd = r'/Users/Alina/Documents/shopXOTest/tesseract'
# 添加路徑
os.environ['TESSDATA_PREFIX'] = r'/Users/Alina/Documents/shopXOTest'
scrpath = "/Users/Alina/Documents/shopXOTest"
if Path(scrpath).is_dir(): #判斷文件夾路徑是否已經(jīng)存在
pass
else:
Path(scrpath).mkdir() #如果不存在经瓷,創(chuàng)建文件夾
browser = webdriver.Chrome()
browser.get('https://demo.shopxo.net/')
browser.refresh()
browser.set_window_size(1200, 800)
# 關(guān)閉廣告
adEle = WebDriverWait(browser,10).until(lambda browser:browser.find_element_by_css_selector("a.am-close.am-close-alt.am-close-spin.am-icon-times.am-fr.submit-ajax"))
adEle.click()
# 點(diǎn)擊注冊(cè)
regEle = WebDriverWait(browser,10).until(lambda browser:browser.find_element_by_css_selector("ul.top-nav-left>div>div>a:nth-child(4)"))
# regEle.click()
browser.execute_script("arguments[0].click();", regEle)
# 輸入用戶名
usernameEle = WebDriverWait(browser,10).until(lambda browser:browser.find_element_by_css_selector("body > div.am-g.my-content.user-register-container > div > div > div > div.register-content > div.am-tabs.am-tabs-d2.am-no-layout > div > div.am-tab-panel.am-active > form > div:nth-child(1) > input"))
usernameEle.send_keys(usernameValue)
# 輸入登錄密碼
pwEle = WebDriverWait(browser,10).until(lambda browser:browser.find_element_by_css_selector(".am-form.form-validation-username>div:nth-child(2)>div>input"))
pwEle.send_keys("123456")
# 驗(yàn)證碼
# imgsrc = browser.find_element_by_id("form-verify-img").get_attribute('src')
# if re.match('https://demo.shopxo.net/index/user/userverifyentry/type/images_verify_reg.html', imgsrc):
# print("需要驗(yàn)證碼")
# else:
# print("不需要驗(yàn)證碼")
location = browser.find_element_by_css_selector('.am-form.form-validation-username>div:nth-child(3)>div>span>span>img').location
size = browser.find_element_by_css_selector('.am-form.form-validation-username>div:nth-child(3)>div>span>span>img').size
# top = location['y'] + 170
#將頁(yè)面滾動(dòng)條拖到能展示驗(yàn)證碼的位置
js="var q=document.documentElement.scrollTop=170"
# browser.execute_script(js)
browser.save_screenshot(screenImg1)
#定位驗(yàn)證碼位置及大小
# location = browser.find_element_by_css_selector('.am-form.form-validation-username>div:nth-child(3)>div>span>span>img').location
print(location)
# size = browser.find_element_by_css_selector('.am-form.form-validation-username>div:nth-child(3)>div>span>span>img').size
print(size)
left = location['x']
top = location['y']
right = location['x'] + size['width']
bottom = location['y'] + size['height']
# #瀏覽器頁(yè)面截屏
# browser.get_screenshot_as_file(screenImg)
#從文件讀取截圖爆哑,截取驗(yàn)證碼位置再次保存
img = Image.open(screenImg1).crop((left,top,right,bottom))
img = img.convert('L') #轉(zhuǎn)換模式:L | RGB
img = ImageEnhance.Contrast(img)#增強(qiáng)對(duì)比度
img = img.enhance(2.0) #增加飽和度
img.save(screenImg2)
#再次讀取識(shí)別驗(yàn)證碼
print("識(shí)別出的結(jié)果如下:")
img = Image.open(screenImg2)
code = pytesseract.image_to_string(img)
print(code)
#code= pytesser.image_file_to_string(screenImg)
browser.find_element_by_css_selector(".am-form.form-validation-username>div:nth-child(3)>div>input").send_keys(code.strip())
print(code.strip())
# 勾選同意
browser.find_element_by_css_selector("form.am-form.form-validation-username>div:nth-child(4)>label>span>i:nth-child(2)").click()
# 點(diǎn)擊【注冊(cè)】
browser.find_element_by_css_selector("form.am-form.form-validation-username>div:nth-child(5)>button").click()