python實現百萬答題自動百度搜尋答案,python百度搜尋

來源:互聯網
上載者:User

python實現百萬答題自動百度搜尋答案,python百度搜尋

用python搭建百萬答題、自動百度搜尋答案。

使用平台

windows7
python3.6
MIX2手機

代碼原理

手機螢幕內容同步到pc端
對問題
對文字分析
用瀏覽器自動搜尋文本

使用教程

1、使用Airdroid 將手機螢幕顯示在電腦螢幕上。也可使用360手機小幫手實現。不涉及任何代碼。實現效果

2、在提問出現時,運行python程式,將問題部分。

這裡要用到兩個函數:

get_point()  #採集要的座標,以及圖片的高度寬度
window_capture()   #

def get_point():  '''''採集座標,並返回w,h,x,y。 作為window_capture() 函數使用'''  try:  print('正在採集座標1,請將滑鼠移動到該點')  # print(3)  # time.sleep(1)  print(2)  time.sleep(1)  print(1)  time.sleep(1)  x1,y1 = pag.position() #返回滑鼠的座標  print('採集成功,座標為:',(x1,y1))  print('')  # time.sleep(2)  print('正在採集座標2,請將滑鼠移動到該點')  print(3)  time.sleep(1)  print(2)  time.sleep(1)  print(1)  time.sleep(1)  x2, y2 = pag.position() # 返回滑鼠的座標  print('採集成功,座標為:',(x2,y2))  #os.system('cls')#清除螢幕  w = abs(x1 - x2)  h = abs(y1 - y2)  x = min(x1, x2)  y = min(y1, y2)  return (w,h,x,y)  except KeyboardInterrupt:  print('擷取失敗') 
def window_capture(result,filename):  '''''擷取'''  #寬度w  #高度h  #左上方的座標x,y  w,h,x,y=result  hwnd = 0  hwndDC = win32gui.GetWindowDC(hwnd)  mfcDC = win32ui.CreateDCFromHandle(hwndDC)  saveDC = mfcDC.CreateCompatibleDC()  saveBitMap = win32ui.CreateBitmap()  MoniterDev = win32api.EnumDisplayMonitors(None,None)  #w = MoniterDev[0][2][2]  # #h = MoniterDev[0][2][3]  # w = 516  # h = 514  saveBitMap.CreateCompatibleBitmap(mfcDC,w,h)  saveDC.SelectObject(saveBitMap)  saveDC.BitBlt((0,0),(w,h),mfcDC,(x,y),win32con.SRCCOPY)  saveBitMap.SaveBitmapFile(saveDC,filename) 

運行後如下

3.對圖片文字分析提取

參考連結: * 圖片轉文本 * 配置方式

代碼部分:

def orc_pic():  #識別中文  text=pytesseract.image_to_string(Image.open('jietu.jpg'),lang='chi_sim')  #識別英文  # text=pytesseract.image_to_string(Image.open('jietu.jpg'))  text = ''.join(text.split())  return text 

4.對文本進行搜尋

 #瀏覽器搜尋url = 'http://www.baidu.com/s?wd=%s' % textwebbrowser.open(url)

所有代碼如下:

 #coding:'utf-8'import win32gui, win32ui, win32con, win32apifrom PIL import Imageimport pytesseractimport webbrowser#先下載pyautogui庫,pip install pyautoguiimport os,timeimport pyautogui as pag#擷取sdk http://ai.baidu.com/。#擷取aip pip install git+https://github.com/Baidu-AIP/python-sdk.git@masterfrom aip import AipOcrimport jsonstatus=0""" 你的 APPID AK SK """APP_ID = '****'API_KEY = '***'SECRET_KEY = '***'client = AipOcr(APP_ID, API_KEY, SECRET_KEY)""" 讀取圖片 """def get_question(path): '''百度識別圖片文字''' with open(path, 'rb') as fp: image=fp.read() res = client.basicGeneral(image) words = res['words_result'] lines = [item['words'] for item in words] question = ''.join(lines) if question[1] == '.': question = question[2:] elif question[2] == '.': question = question[3:] return question.replace('?', ' ')#採集座標def get_point(): '''採集座標,並返回w,h,x,y。 作為window_capture() 函數使用''' try: print('正在採集座標1,請將滑鼠移動到該點') # print(3) # time.sleep(1) print(2) time.sleep(1) print(1) time.sleep(1) x1,y1 = pag.position() #返回滑鼠的座標 print('採集成功,座標為:',(x1,y1)) print('') # time.sleep(2) print('正在採集座標2,請將滑鼠移動到該點') print(3) time.sleep(1) print(2) time.sleep(1) print(1) time.sleep(1) x2, y2 = pag.position() # 返回滑鼠的座標 print('採集成功,座標為:',(x2,y2)) #os.system('cls')#清除螢幕 w = abs(x1 - x2) h = abs(y1 - y2) x = min(x1, x2) y = min(y1, y2) return (w,h,x,y) except KeyboardInterrupt: print('擷取失敗')#擷取def window_capture(result,filename): '''擷取''' #寬度w #高度h #左上方的座標x,y w,h,x,y=result hwnd = 0 hwndDC = win32gui.GetWindowDC(hwnd) mfcDC = win32ui.CreateDCFromHandle(hwndDC) saveDC = mfcDC.CreateCompatibleDC() saveBitMap = win32ui.CreateBitmap() MoniterDev = win32api.EnumDisplayMonitors(None,None) #w = MoniterDev[0][2][2] # #h = MoniterDev[0][2][3] # w = 516 # h = 514 saveBitMap.CreateCompatibleBitmap(mfcDC,w,h) saveDC.SelectObject(saveBitMap) saveDC.BitBlt((0,0),(w,h),mfcDC,(x,y),win32con.SRCCOPY) saveBitMap.SaveBitmapFile(saveDC,filename)def get_point_txt(status): #如果status=y,則重新擷取座標 '''如果存在point.txt,則詢問是否重新採集,刪除point.txt;如果不存在txt,則直接採集。''' if not os.path.isfile('point.txt') : result = get_point() with open('point.txt', 'w') as f: f.write(str(result)) return result else: if status=='y': result = get_point() with open('point.txt', 'w') as f: f.write(str(result)) return result else: with open('point.txt', 'r') as f: result = f.readline() result = eval(result) return resultdef orc_pic(): #識別中文 text=pytesseract.image_to_string(Image.open('jietu.jpg'),lang='chi_sim') #識別英文 # text=pytesseract.image_to_string(Image.open('jietu.jpg')) text = ''.join(text.split()) return text#百度識別def orc_baidu(): text=get_question('jietu.jpg') return textstatus='y'start = time.time()result=get_point_txt(status)for i in range(10): window_capture(result,'jietu.jpg')# text=orc_baidu()text=orc_pic()print(text)#瀏覽器搜尋url = 'http://www.baidu.com/s?wd=%s' % textwebbrowser.open(url)# url2='https://www.google.com/search?q=%s' % text# webbrowser.open(url2)end = time.time()time=end-startprint('此次耗時%.1f秒' % time)

以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支援幫客之家。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.