侯体宗的博客
  • 首页
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

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

Python  /  管理员 发布于 7年前   230

用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' % textwebbrowser.open(url)

所有代码如下:

 #coding:'utf-8'import win32gui, win32ui, win32con, win32apifrom PIL import Imageimport pytesseractimport webbrowser#先下载pyautogui库,pip install pyautoguiimport os,timeimport pyautogui as pag#获取sdk http://ai.baidu.com/。#获取aip pip install git+https://github.com/Baidu-AIP/python-sdk.git@masterfrom aip import AipOcrimport jsonstatus=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 resultdef 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 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)#浏览器搜索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('此次耗时%.1f秒' % time)

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


  • 上一条:
    答题辅助python代码实现
    下一条:
    Python数据结构之双向链表的定义与使用方法示例
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 在python语言中Flask框架的学习及简单功能示例(0个评论)
    • 在Python语言中实现GUI全屏倒计时代码示例(0个评论)
    • Python + zipfile库实现zip文件解压自动化脚本示例(0个评论)
    • python爬虫BeautifulSoup快速抓取网站图片(1个评论)
    • vscode 配置 python3开发环境的方法(0个评论)
    • 近期文章
    • 在windows10中升级go版本至1.24后LiteIDE的Ctrl+左击无法跳转问题解决方案(0个评论)
    • 智能合约Solidity学习CryptoZombie第四课:僵尸作战系统(0个评论)
    • 智能合约Solidity学习CryptoZombie第三课:组建僵尸军队(高级Solidity理论)(0个评论)
    • 智能合约Solidity学习CryptoZombie第二课:让你的僵尸猎食(0个评论)
    • 智能合约Solidity学习CryptoZombie第一课:生成一只你的僵尸(0个评论)
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(95个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2016-11
    • 2018-04
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2022-01
    • 2023-07
    • 2023-10
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客