打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
python实现百万答题自动百度搜索答案

2021-01-05 00:22书包的StoryPython

这篇文章主要为大家详细介绍了python实现百万答题百度搜索,有一定的参考价值,另外的小伙伴可以看看

用百度回答百万答题、自动。

使用平台

windows7
python3.6
MIX2手机

代码原理

手机屏幕内容同步到pc端
对问题
截图分析
用自动浏览器

使用教程

1、使用Airdroid将手机屏幕显示在电脑上。也可以使用360助手。不涉及任何代码。实现效果如图:

2、在提问出现时,运行python程序,将部分截图。

这里要到两个函数:

get_point() #采集要截图的坐标,以及图片的高度宽度
window_capture() #截图

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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('获取失败')
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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.对图片文字分析提取

参考链接: *图片转文本*配置方式

代码部分:

?
1
2
3
4
5
6
7
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.对文本进行搜索

?
1
2
3
#浏览器搜索
url = 'http://www.baidu.com/s?wd=%s' % text
webbrowser.open(url)

所有代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#coding:'utf-8'
import win32gui, win32ui, win32con, win32api
from PIL import Image
import pytesseract
import webbrowser
#先下载pyautogui库,pip install pyautogui
import os,time
import pyautogui as pag
#获取sdk http://ai.baidu.com/。
#获取aip pip install git+https://github.com/Baidu-AIP/python-sdk.git@master
from aip import AipOcr
import json
status=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 result
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
#百度识别
def orc_baidu():
 text=get_question('jietu.jpg')
 return text
status='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' % text
webbrowser.open(url)
# url2='https://www.google.com/search?q=%s' % text
# webbrowser.open(url2)
end = time.time()
time=end-start
print('此次耗时%.1f秒' % time)

以上就是本文的全部内容,希望对大家的学习有帮助,希望大家多多之家。

译文链接:http://blog.csdn.net/m0_37854650/article/details/79052911

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
(1)截屏函数,调用方法window
Python实现屏幕截图的两种方式
一键翻译自动填表工具
Python3.6实现12306火车票自动抢票
使用python开发钉钉自动打卡程序(python自动化)
Python 利用线程实现两个以上while 1循环同时执行
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服