用 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' % 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搭建百万赢家自动搜索答案相关推荐

  1. python 网页上显示数据_用Python实现网页数据抓取

    需求: 获取某网站近10万条数据记录的相关详细信息. 分析:数据的基本信息存放于近1万个页面上,每个页面上10条记录.如果想获取特定数据记录的详细信息,需在基本信息页面上点击相应记录条目,跳转到详细信 ...

  2. python autoit上传文件_结合python+selenium使用AutoIt V3实现文件、图片上传

    1.下载.安装AutoIt V3 下载官网:https://www.autoitscript.com/site/autoit/downloads/ 2.AutoIt V3组件介绍 AutoIt Win ...

  3. python网站用什么数据库_使用python读取mysql数据库并进行数据的操作

    简介:(一)环境的配置使用python调用mysql数据库要引进一些库.目前我使用的python版本是python3.6.引进库为pymysql其他对应的库可以有以下选择: mysqldb,oursq ...

  4. python怎么另起一行阅读答案_使用Python+Dlib构建人脸识别系统(在Nvidia Jetson Nano 2GB开发板上)...

    Nvidia Jetson Nano 2GB开发板是一款新的单板机 售价59美元 运行带有GPU加速的人工智能软件.在2020年 你可以从一台售价59美元的单板计算机中获得令人惊叹的性能 让我们用它来 ...

  5. 可以搜python编程答案的软件_python实现百万答题自动百度搜索答案

    用python搭建百万答题.自动百度搜索答案. 使用平台 windows7 python3.6 MIX2手机 代码原理 手机屏幕内容同步到pc端 对问题截图 对截图文字分析 用浏览器自动搜索文本 使用 ...

  6. 利用python从网页查找数据_利用Python模拟淘宝的搜索过程并对数据进行可视化分析...

    数据挖掘入门与实战 公众号: datadw 本文讲述如何利用Python模拟淘宝的搜索过程并对搜索结果进行初步的数据可视化分析. 搜索过程的模拟:淘宝的搜索页面有两种形式, 一种形式是, 2019/2 ...

  7. python爬取电影评分_用Python爬取猫眼上的top100评分电影

    代码如下: # 注意encoding = 'utf-8'和ensure_ascii = False,不写的话不能输出汉字 import requests from requests.exception ...

  8. python 定时执行 爬虫 模块_【Python】定时执行网站爬虫

    今天我们额讨论如何使用Python,SQLite数据库与crontab工具将爬虫程序部署到服务器上并实现定时爬取存储 编写爬虫代码 编写一个爬虫程序,使用requests与beautifulsoup4 ...

  9. python在人工智能应用锁_干货 | Python人工智能在贪吃蛇游戏中的应用探索(上)...

    文案&代码 白宇啸 排版&审校 邓发珩 前言 一个月前,人工智能对我来说都是很陌生的,更不用说神经网络.强化学习.DQN等名词了.疫情期间,经过在家努力学习,我对这些概念越来越清晰了, ...

  10. python程序设计教程杨年华课后答案_《PYTHON程序设计》习题与答案.pdf

    <Python 程序设计>习题与参考答案 1 第 章 基础知识 1.1 简单说明如何选择正确的Python 版本. 答: 在选择Python 的时候,一定要先考虑清楚自己学习Python ...

最新文章

  1. mysql libs 冲突_mysql-libs的冲突
  2. 使用common-fileUpload制作文件上传【转】
  3. adb 命令 pc端 复制粘贴 文本到android设备
  4. 使用git remote提交代码
  5. git 清除 另一个git进程似乎在这个仓库中运行。。。。。
  6. javaEE的开发模式
  7. PHP pathinfo() 函数【获取文件后缀】
  8. Android内核开发:从源码树中删除出厂的app应用
  9. dubbo的基于java的路由_1 | Dubbo:探讨标签路由的实现
  10. 多智能体强化学习(二) MAPPO算法详解
  11. 头歌平台(EduCoder)—— 数据挖掘算法原理与实践:数据预处理
  12. 【网络】Padavan 路由器固件设置打印机服务器
  13. 工具推荐——Snipaste
  14. 计算机中怎样重新安装ps,什么样的电脑适合安装高版本Photoshop?该如何安装和卸载PS呢?...
  15. iOS依赖注入框架系列(三):模块化Typhoon
  16. 关于监听Android的静音键以及音量按键
  17. 2017 ACM Arabella Collegiate Programming Contest(solved 11/13)
  18. 有没有免费的视频剪辑软件?快来看看这些视频裁剪软件
  19. 用Javascript删除HTML元素
  20. DMZ主机的使用设置

热门文章

  1. BZOJ3813 奇数国
  2. 实时查询(otoci)
  3. veevalidate的使用3.X版本
  4. 利用before伪元素创建图标
  5. 小程序学习笔记(8)-导航组件
  6. Oracle密码过期怎么办
  7. java访问带有密码验证的es_elasticsearch x_pack带验证 java配置访问
  8. python推荐系统算法朴素贝叶斯_Java实现的朴素贝叶斯算法示例
  9. pythonsuper多重继承_解决python super()调用多重继承函数的问题
  10. redis 获取不到_redis系列之——缓存穿透、缓存击穿、缓存雪崩