OCR (光学字符识别技术)安装
一、安装homebrew
1)打开终端直接输入安装命令:
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
2)安装完成,验证是否安装成功,输入查询版本命令:
brew –version
注意安装时偶遇到相关问题:
安装完毕后,发现使用brew命令,却总是提示:-bash: brew: command not found
通过排查,发现/usr/local/bin不在PATH中,这里需要进行文件配置操作
1、终端输入:sudo vim .bash_profile(回车后进入修改状态)
2、添加:PATH=“.;$PATH:/usr/local/bin”
3、esc退出命令, :wq 保存文件并退出vi
4、source .bash_profile使配置修改生效
再次使用brew命令即可。
https://docs.brew.sh—使用帮助,参考官方文档!
二、安装 Imagemagick 和 Tesseract 库
brew install imagemagick
brew install tesseract
安装截图:
三、安装Selenium和ChromeDriver
1、终端输入:
(win)pip install selenium
(linux) pip3 install selenium
pip3 list selenium
2、下载对应的ChromeDriver
下载地址:http://npm.taobao.org/mirrors/chromedriver/
3、验证是否安装成功
chromedriver Starting ChromeDriver 2.41.578706 (5f725d1b4f0a4acbf5259df887244095596231db) on port 9515 Only local connections are allowed.
4.代码测试
from selenium import webdriver browser = webdriver.Chrome() browser.get(\'https://www.baidu.com\') 运行结果:
5.通过OCR识别验证码图片(实例)
import pytesseract as pytesseract from selenium import webdriver import time from PIL import Image browser = webdriver.Chrome() browser.get(\'https://xxxxxxx\') time.sleep(6) browser.find_element_by_id("userName").send_keys("xxxxxxx") browser.find_element_by_id("pwd").send_keys("xxxxxx") time.sleep(6) codeElement=browser.find_element_by_id("pin")#1、获取验证码输入框 imgElement = browser.find_element_by_class_name("login-pin-img")#2、获取验证码图片 browser.save_screenshot(\'/Users/pangyongjuan/Documents/test/01.png\')#3、 截取全屏 yanzhenga = browser.find_element_by_class_name("login-pin-img") left = yanzhenga.location[\'x\']#4、 通过location定位x,y top = yanzhenga.location[\'y\'] elementWidth = yanzhenga.location[\'x\'] + yanzhenga.size[\'width\']#5、 通过x,y的值拼接长和宽 elementHeight = yanzhenga.location[\'y\'] + yanzhenga.size[\'height\'] picture = Image.open(\'/Users/pangyongjuan/Documents/test/01.png\')#6、 打开刚截取的全屏图 picture = picture.crop((left, top, elementWidth, elementHeight))#7、# 定位到需要截取的地方 picture.save(\'/Users/pangyongjuan/Documents/test/02.png\')#8、 截取成功并保存到本地