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


Posted in Python onJanuary 16, 2018

用python搭建百万答题、自动百度搜索答案。

使用平台

windows7
python3.6
MIX2手机

代码原理

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

使用教程

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

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

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

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)

运行后截图如下

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

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' % text
webbrowser.open(url)

所有代码如下:

#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)

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
讲解Python中fileno()方法的使用
May 24 Python
Python捕捉和模拟鼠标事件的方法
Jun 03 Python
Python正则表达式分组概念与用法详解
Jun 24 Python
使用python编写udp协议的ping程序方法
Apr 22 Python
python3实现钉钉消息推送的方法示例
Mar 14 Python
Python静态类型检查新工具之pyright 使用指南
Apr 26 Python
PyQt5 对图片进行缩放的实例
Jun 18 Python
Python *args和**kwargs用法实例解析
Mar 02 Python
keras使用Sequence类调用大规模数据集进行训练的实现
Jun 22 Python
django form和field具体方法和属性说明
Jul 09 Python
linux centos 7.x 安装 python3.x 替换 python2.x的过程解析
Dec 14 Python
Python实战之实现康威生命游戏
Apr 26 Python
Python数据结构之双向链表的定义与使用方法示例
Jan 16 #Python
python+pillow绘制矩阵盖尔圆简单实例
Jan 16 #Python
Python面向对象编程之继承与多态详解
Jan 16 #Python
Python基于socket实现简单的即时通讯功能示例
Jan 16 #Python
python中将字典形式的数据循环插入Excel
Jan 16 #Python
python+tkinter编写电脑桌面放大镜程序实例代码
Jan 16 #Python
详解python函数传参是传值还是传引用
Jan 16 #Python
You might like
乐信RP2100的电路分析和打磨
2021/03/02 无线电
怎么在Windows系统中搭建php环境
2013/08/31 PHP
PHP中file_put_contents追加和换行的实现方法
2017/04/01 PHP
PHP命令空间namespace及use的用法小结
2017/11/27 PHP
php如何把表单内容提交到数据库
2019/07/08 PHP
基于jQuery替换table中的内容并显示进度条的代码
2011/08/02 Javascript
event.X和event.clientX的区别分析
2011/10/06 Javascript
如何使用Javascript正则表达式来格式化XML内容
2013/07/04 Javascript
js导出格式化的excel 实例方法
2013/07/17 Javascript
浅析XMLHttpRequest的缓存问题
2013/12/13 Javascript
JS+DIV+CSS实现仿表单下拉列表效果
2015/08/18 Javascript
JavaScript实现的微信二维码图片生成器的示例
2016/10/26 Javascript
AngularJS页面传参的5种方式
2017/04/01 Javascript
详解微信小程序Page中data数据操作和函数调用
2017/09/27 Javascript
关于HTTP传输中gzip压缩的秘密探索分析
2018/01/12 Javascript
详解vue-cli 快速搭建单页应用之遇到的问题及解决办法
2018/03/01 Javascript
基于vue 添加axios组件,解决post传参数为null的问题
2018/03/05 Javascript
对vue2.0中.vue文件页面跳转之.$router.push的用法详解
2018/08/24 Javascript
uni-app使用微信小程序云函数的步骤示例
2020/05/22 Javascript
前端 javascript 实现文件下载的示例
2020/11/24 Javascript
[07:54]DOTA2 MV《我的动力鞋》 ImbaTV 出品
2014/11/21 DOTA
python动态网页批量爬取
2016/02/14 Python
python线程池threadpool实现篇
2018/04/27 Python
pytorch使用指定GPU训练的实例
2019/08/19 Python
opencv实现简单人脸识别
2021/02/19 Python
如何基于Python实现word文档重新排版
2020/09/29 Python
python 写一个文件分发小程序
2020/12/05 Python
HTML5 Canvas画线技巧——实现绘制一个像素宽的细线
2013/08/02 HTML / CSS
Joseph官网:英国小众奢侈品牌
2019/05/17 全球购物
请写出 BOOL flag 与"零值"比较的 if 语句
2016/02/29 面试题
男方父母证婚词
2014/01/12 职场文书
养殖项目策划书范文
2014/01/13 职场文书
岗位廉洁从政承诺书
2014/03/27 职场文书
党的群众路线对照检查材料思想汇报
2014/09/25 职场文书
入党自传范文2015
2015/06/26 职场文书
日本十大血腥动漫,那些被禁播的动漫盘点
2022/03/21 日漫