大家有没有遇到过这样的问题:当我们看到有些图片中的文字(比如论文中的图片或公式等),想搜索一下,但苦于不能直接复制,只能手动敲入在百度的搜索框;或者碰到其他复制比较麻烦的地方,比如cmd窗口,也是如此.这样费时费力,还可能会输入错误。
因此我尝试用python的验证码识别模块tesseract和自动化测试模块selenium写了个很简单的python程序,可以将想要搜索的文字截图下来保存在python程序当前目录下,运行Python程序就会调用tesseract识别然后selenium自动百度之,成功率还挺高的,以下是完整代码:
# -*- coding: utf-8 -*-
import pytesseract as rec
from PIL import Image
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
def verify(filename):
#识别截图并返回字符串
image = Image.open(filename)
code = rec.image_to_string(image)
return code
#得到cmd窗口中的关键字
keyword = verify('cmd.png')
#打开浏览器并百度上述关键字
browser = w