使用了以下軟件: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()