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生成指定长度的随机数密码
Jan 23 Python
解析Python中while true的使用
Oct 13 Python
Python工程师面试题 与Python基础语法相关
Jan 14 Python
Python教程之全局变量用法
Jun 27 Python
Python 反转字符串(reverse)的方法小结
Feb 20 Python
Python动态导入模块的方法实例分析
Jun 28 Python
python 将print输出的内容保存到txt文件中
Jul 17 Python
python数据处理 根据颜色对图片进行分类的方法
Dec 08 Python
FFrpc python客户端lib使用解析
Aug 24 Python
基于selenium及python实现下拉选项定位select
Jul 22 Python
python通过函数名调用函数的几种场景
Sep 23 Python
如何使用pdb进行Python调试
Jun 30 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
php生成随机密码的三种方法小结
2010/09/04 PHP
php实现汉字验证码和算式验证码的方法
2015/03/07 PHP
PHP的伪随机数与真随机数详解
2015/05/27 PHP
隐藏Nginx或Apache以及PHP的版本号的方法
2016/01/03 PHP
PHP序列化/对象注入漏洞分析
2016/04/18 PHP
php将服务端的文件读出来显示在web页面实例
2016/10/31 PHP
修改yii2.0用户登录使用的user表为其它的表实现方法(推荐)
2017/08/01 PHP
JS按位非(~)运算符与~~运算符的理解分析
2011/07/31 Javascript
JavaScript 处理Iframe自适应高度(同或不同域名下)
2013/03/29 Javascript
jquery 重写 ajax提交并判断权限后 使用load方法报错解决方法
2016/01/19 Javascript
浅谈jquery点击label触发2次的问题
2016/06/12 Javascript
手机端图片缩放旋转全屏查看PhotoSwipe.js插件实现
2016/08/25 Javascript
js中toString()和String()区别详解
2017/03/23 Javascript
Angular.js中ng-include用法及多标签页面的实现方式详解
2017/05/07 Javascript
JS倒计时实例_天时分秒
2017/08/22 Javascript
js实现随机点名系统(实例讲解)
2017/10/18 Javascript
JavaScript 格式化数字、金额、千分位、保留几位小数、舍入舍去
2019/07/23 Javascript
vue之延时刷新实例
2019/11/14 Javascript
超简单的微信小程序轮播图
2019/11/22 Javascript
Vue文本模糊匹配功能如何实现
2020/07/30 Javascript
举例讲解Python的lambda语句声明匿名函数的用法
2016/07/01 Python
对python读取CT医学图像的实例详解
2019/01/24 Python
详解python做UI界面的方法
2019/02/27 Python
django celery redis使用具体实践
2019/04/08 Python
Python实现带下标索引的遍历操作示例
2019/05/30 Python
python爬虫分布式获取数据的实例方法
2020/11/26 Python
详解python定时简单爬取网页新闻存入数据库并发送邮件
2020/11/27 Python
Nº21官方在线商店:numeroventuno.com
2019/09/26 全球购物
毕业生造价工程师求职信
2013/10/17 职场文书
中学教师管理制度
2014/01/14 职场文书
自荐信写法介绍
2014/01/25 职场文书
新闻工作者先进事迹
2014/05/26 职场文书
办公室禁烟通知
2015/04/23 职场文书
小学工作总结2015
2015/05/04 职场文书
2015年端午节活动策划书
2015/05/05 职场文书
小学语文教师竞聘演讲稿范文
2019/08/09 职场文书