安装pytesseract https://www.jianshu.com/p/2db541800418
通过cmd输入pip install pytesseract进行安装,但是安装后并不能直接使用,还需要下载Tesseract-OCR。
下载Tesseract-OCR
下载完双击打开,连续next,直到出现安装路径的时候,可以自定义安装路径也可以使用默认的安装路径,但是无论是哪一种一定要记住路径。
然后通过cmd输入pip install pytesseract可以看到自己安装的pytesseract所在路径
根据路径找到pytesseract.py
点开编辑,找到tesseract_cmd将它改为你刚刚安装的tesseract的路径。
保存后去运行程序会发现没办法使用pytesseract库,它还是会报错,这是由于环境变量也要进行设置。
点开我的电脑—》属性—》高级系统设置—》环境变量,新建一个变量:
路径还是你刚刚安装的Tesseract-OCR路径,但是要将它定位到其中的tessdata,变量名也一定不能改。
然后在下面的path中加入的变量:
保存后,请一定要重启,然后在去运行程序就可以使用pytesseract库了。
代码
// 使用selenium 对节点进行截图
driver.find_element_by_xpath('').screenshot('')
# 使用pytesseract 解析图片
from openpyxl.drawing.image import Image
import pytesseract
from PIL import Image
def imge_to_text(path):
tessdata_dir_config = '--tessdata-dir "c://Program Files//Tesseract-OCR//tessdata"'
pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'
image = Image.open(path)
text = pytesseract.image_to_string(image, lang='chi_sim', config=tessdata_dir_config)
return text
了解更多分析及数据抓取可查看:
http://cloud.yisurvey.com:9081/html/d8acf20b-342f-4806-8dcc-5e6a8d00881d.html?ly=csdn