使用了以下软件:1、雷电安卓模拟器+知乎最新版apk 用来截图
2、tesseract 用来将图片转换成了文字
3、selenium操作chrome浏览器搜索答案
注:这是不实用的版本,因为知乎只给10s的反应时间,而程序大概要跑7s,并且百度搜索答案很不准确。但这个思路使用与其他答题软件,只要时间给的比较充裕。
其次,此程序只是用来学习,实际运行过程中会出现tessert库多次崩溃,我没有解决。因此并不实用。
改进策略:1、将识别后的文字分词,然后爬取所有页面进行TF-IDF分析,找出权重最高的一组候选解
2、使用其他方法识别文字,因为tesseract库比较慢,总之尽可能缩短时间
import os import time from selenium import webdriver from PIL import Image import pytesseract class King: num=0 path=r"C:\Users\Administrator\Documents\雷电模拟器\Pictures\Screenshots" browser = webdriver.Chrome() browser.get("http://www.baidu.com") def __init__(self): while True: while True: self.detect() if self.num != 0: break print(self.num) self.web() def detect(self): self.num = len(os.listdir(r"C:\Users\Administrator\Documents\雷电模拟器\Pictures\Screenshots")) time.sleep(0.1) def getname(self): return str(os.listdir(r"C:\Users\Administrator\Documents\雷电模拟器\Pictures\Screenshots")[0]) def getInformation(self,path): img = Image.open(path) region = (50, 300, 600, 430) # 裁切图片 cropImg = img.crop(region) cropImg.show() text = pytesseract.image_to_string(cropImg, lang='chi_sim') time.sleep(0.1) text = text.replace(" ", "").replace('\n', "") return text def web(self): if self.num is 1: name=self.getname() #pinjie path temppath=self.path+'\\'+name text=self.getInformation(temppath) self.browser.find_element_by_class_name("s_ipt").send_keys(text) self.browser.find_element_by_id("su").click() while True: self.detect() if self.num is 2: self.browser.find_element_by_class_name("s_ipt").clear() break os.remove(temppath) King()