1、安裝homebrewpython
1)打開終端直接輸入安裝命令:linux
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
2)安裝完成,驗證是否安裝成功,輸入查詢版本命令:git
brew --versiongithub
注意安裝時偶遇到相關問題:web
安裝完畢後,發現使用brew命令,卻老是提示:-bash: brew: command not foundchrome
經過排查,發現/usr/local/bin不在PATH中,這裏須要進行文件配置操做npm
一、終端輸入:sudo vim .bash_profile(回車後進入修改狀態)vim
二、添加:PATH=「.;$PATH:/usr/local/bin」ruby
三、esc退出命令, :wq 保存文件並退出vibash
四、source .bash_profile使配置修改生效
再次使用brew命令便可。
https://docs.brew.sh---使用幫助,參考官方文檔!
2、安裝 Imagemagick 和 Tesseract 庫
brew install imagemagick
brew install tesseract
安裝截圖:
3、安裝Selenium和ChromeDriver
一、終端輸入:
(win)pip install selenium
(linux) pip3 install selenium
pip3 list selenium
二、下載對應的ChromeDriver
下載地址:http://npm.taobao.org/mirrors/chromedriver/
三、驗證是否安裝成功
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")#一、獲取驗證碼輸入框 imgElement = browser.find_element_by_class_name("login-pin-img")#二、獲取驗證碼圖片 browser.save_screenshot('/Users/pangyongjuan/Documents/test/01.png')#三、 截取全屏 yanzhenga = browser.find_element_by_class_name("login-pin-img") left = yanzhenga.location['x']#四、 經過location定位x,y top = yanzhenga.location['y'] elementWidth = yanzhenga.location['x'] + yanzhenga.size['width']#五、 經過x,y的值拼接長和寬 elementHeight = yanzhenga.location['y'] + yanzhenga.size['height'] picture = Image.open('/Users/pangyongjuan/Documents/test/01.png')#六、 打開剛截取的全屏圖 picture = picture.crop((left, top, elementWidth, elementHeight))#七、# 定位到須要截取的地方 picture.save('/Users/pangyongjuan/Documents/test/02.png')#八、 截取成功並保存到本地