Python implements an automatic Baidu search for answers for millions of answers, and python Baidu search

Source: Internet
Author: User

Python implements an automatic Baidu search for answers for millions of answers, and python Baidu search

Use python to set up million answers and search for answers automatically by Baidu.

Platform

Windows 7
Python3.6
MIX2 mobile phone

Code principle

Synchronize mobile phone screen content to pc
Question
Text Analysis
Automatically search for text in a browser

Tutorial

1. Use Airdroid to display the mobile phone screen on the computer screen. You can also use the 360 mobile assistant. No code is involved. Effect

2. When a question is asked, run the python program.

Two functions are used here:

Get_point () # Collect the coordinates and the height and width of the image
Window_capture ()#

Def get_point (): ''' collects coordinates and returns w, h, x, and y. As the window_capture () function, use ''try: print ('coordinate 1 is being collected. move the cursor to this point') # print (3) # time. sleep (1) print (2) time. sleep (1) print (1) time. sleep (1) x1, y1 = pag. position () # Return the cursor coordinate print ('collection successful, coordinate: ', (x1, y1) print ('') # time. sleep (2) print ('collecting coordinate 2, please move the mouse to this point') print (3) time. sleep (1) print (2) time. sleep (1) print (1) time. sleep (1) x2, y2 = pag. position () # Return the cursor coordinate print ('acquisition successful, coordinate: ', (x2, y2) # OS. system ('cls') # clear screen w = abs (x1-x2) h = abs (y1-y2) x = min (x1, x2) y = min (y1, y2) return (w, h, x, y) failed t KeyboardInterrupt: print ('get failed ')
Def window_capture (result, filename): ''' get ''' # Width w # height h # coordinate x, y w, h, x in the upper left corner, 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)

After running:

3. Image Text Analysis and Extraction

Reference link: * image to text * configuration method

Code Section:

Def orc_pic (): # recognize Chinese keywords '), lang = 'chi _ sim') # recognize English # text=pytesseract.image_to_string(Image.open('jietu.jpg ') text = ''. join (text. split () return text

4. Search for text

# Search url = 'HTTP: // www.baidu.com/s? Wd = % s' % textwebbrowser. open (url)

All code is as follows:

# Coding: 'utf-8' import win32gui, win32ui, win32con, win32apifrom PIL import Imageimport export webbrowser # first download the pyautogui library, pip install pyautoguiimport OS, timeimport pyautogui as pag # obtain sdk quota /. # Get aip pip install git + https://github.com/Baidu-AIP/python-sdk.git@masterfrom aip import AipOcrimport jsonstatus = 0 "Your appid ak sk" APP_ID = '*** 'api _ KEY = '** * 'secret _ KEY = '*** 'client = AipOcr (APP_ID, API_KEY, SECRET_KEY) "read image" def get_question (path): '''baidu recognized image text''' with open (path, 'rb') as fp: image = fp. read () res = client. basicGeneral (image) words = res ['words _ result'] lines = [item ['w Ords '] for item in words] question = ''. join (lines) if question [1] = '. ': question = question [2:] elif question [2] = '. ': question = question [3:] return question. replace ('? ', '') # Collection coordinate def get_point (): ''' acquisition coordinate, and return w, h, x, y. As the window_capture () function, use ''try: print ('coordinate 1 is being collected. move the cursor to this point') # print (3) # time. sleep (1) print (2) time. sleep (1) print (1) time. sleep (1) x1, y1 = pag. position () # Return the cursor coordinate print ('collection successful, coordinate: ', (x1, y1) print ('') # time. sleep (2) print ('collecting coordinate 2, please move the mouse to this point') print (3) time. sleep (1) print (2) time. sleep (1) print (1) time. sleep (1) x2, y2 = pag. position () # Return the cursor coordinate print ('acquisition successful, coordinate: ', (x2, y2) # OS. system ('cls') # clear screen w = Abs (x1-x2) h = abs (y1-y2) x = min (x1, x2) y = min (y1, y2) return (w, h, x, y) counter t KeyboardInterrupt: print ('failed to get ') # obtain def window_capture (result, filename): ''' get ''' # Width w # height h # coordinate x in the upper left corner, y w, h, x, y = result hwnd = 0 hwndDC = win32gui. getWindowDC (hwnd) mfcDC = win32ui. createDCFromHandle (hwndDC) saveDC = mfcDC. createCompatibleDC () saveBitMap = win32ui. createBitmap () MoniterDev = win32api. enumDisplayMon Itors (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. '''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 (): # recognize Chinese text = pytesseract. image_to_string (Image. o Pen('jietu.jpg '), lang = 'chi _ sim') # identify English # text=pytesseract.image_to_string(Image.open('jietu.jpg ') text = ''. join (text. split () return text # Baidu identification 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) # search 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 ('time consumed %. 1f second' % time)

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.