返回信息流有时遇到图片中的文字想搜索一下,但不能直接复制,或者其他复制比较麻烦的地方比如cmd.
写了个很简单的python程序,可以将想要搜索的文字截图下来保存在python程序当前目录下,运行程序就会调用tesseract识别然后selenium自动百度之,成功率还挺高的,233333
[upload=1][/upload]
这是一条镜像帖。来源:北邮人论坛 / python / #10781同步于 2016/1/4
该镜像源已超过 30 天没有更新,可能在源站已被删除。
Python机器人发帖
发现tesseract结合selenium快速百度图片中的文字挺好用的
iamluo
2016/1/4镜像同步5 回复
订阅后,新回复会通过你的通知中心匿名送达。
5 条回复
上代码
【 在 iamluo (昵称是什么) 的大作中提到: 】
: 有时遇到图片中的文字想搜索一下,但不能直接复制,或者其他复制比较麻烦的地方比如cmd.
: 写了个很简单的python程序,可以将想要搜索的文字截图下来保存在python程序当前目录下,运行程序就会调用tesseract识别然后selenium自动百度之,成功率还挺高的,233333
: [upload=1][/upload]
: ...................
【 在 jh1 的大作中提到: 】
: 上代码
# -*- 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 = webdriver.Firefox()
browser.get('https://www.baidu.com')
try:
search = WebDriverWait(browser,timeout=2).until(EC.presence_of_element_located((By.ID,'kw')),message='timeout')
search.send_keys(keyword)
#模拟点击“百度一下”
browser.find_element_by_class_name('bg s_btn_wr').click()
except NoSuchElementException as e:
assert e.message