前期安装:
1、python3
2、Pillow
3、pytesseract
pip/pip3 install pytesseract
4、tesseract-ocr 下载地址
前期配置:
- 添加环境变量: 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'
验证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"
# 验证码图片保存地址
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(): #判断文件夹路径是否已经存在
pass
else:
Path(scrpath).mkdir() #如果不存在,创建文件夹
browser = webdriver.Chrome()
browser.get('https://demo.shopxo.net/')
browser.refresh()
browser.set_window_size(1200, 800)
# 关闭广告
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()
# 点击注册
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")
# 验证码
# 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("需要验证码")
# else:
# print("不需要验证码")
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
#将页面滚动条拖到能展示验证码的位置
js="var q=document.documentElement.scrollTop=170"
# browser.execute_script(js)
browser.save_screenshot(screenImg1)
#定位验证码位置及大小
# 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']
# #浏览器页面截屏
# browser.get_screenshot_as_file(screenImg)
#从文件读取截图,截取验证码位置再次保存
img = Image.open(screenImg1).crop((left,top,right,bottom))
img = img.convert('L') #转换模式:L | RGB
img = ImageEnhance.Contrast(img)#增强对比度
img = img.enhance(2.0) #增加饱和度
img.save(screenImg2)
#再次读取识别验证码
print("识别出的结果如下:")
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()
# 点击【注册】
browser.find_element_by_css_selector("form.am-form.form-validation-username>div:nth-child(5)>button").click()
网友评论