用 Python 实现手机自动答题,这下百万答题游戏谁也玩不过我!

引言

如果谈到这几年手机上各平台最常见的引流福利,必然是

答题赢大奖系列 小游戏了。像什么头号英雄,百万玩家之类的,充斥在我们生活中,同时也成为了我们生活中常见的娱乐方式。

但是有时候就会想,能不能实现 手机自动答题 呢,毕竟网络上是充斥着很多问题的答案,自己手动搜题速度显然来不及。答案是当然可以 ,今天我们就来用手机连接电脑,让电脑自动搜索答案,省时省力省心。

用 Python 实现手机自动答题,这下百万答题游戏谁也玩不过我!

这一项目中主要用到了 文字识别浏览器 操作,其中文字识别是 利用计算机自动识别字符的技术,是模式识别应用的一个重要领域。

人们在生产和生活中,要处理大量的文字、报表和文本。为了减轻人们的劳动,提高处理效率, 50 年代开始探讨一般文字识别方法,并研制出光学字符识别器。 60 年代出现了采用磁性墨水和特殊字体的实用机器。

60 年代后期,出现了多种字体和手写体文字识别机,其识别精度和机器性能都基本上能满足要求。如用于信函分拣的手写体数字识别机和印刷体英文数字识别机。 70 年代主要研究文字识别的基本理论和研制高性能的文字识别机,并着重于汉字识别的研究 。

基于一些基础认识,下面我们先来思考下,实现这一项目的整体思路:

做这一项目首先会存在以下疑问:

1、 我们要让手机连接电脑,但是怎么让电脑自动控制手机呢

2、 手机上是显示文字的,但是怎么让电脑看懂你手机上的文字呢

3、 电脑知道了问题后如何借助网络搜答案呢?

针对上面的问题,我们大概想了下思路:

1、 让电脑能够控制手机,一般都是利用 usb 把手机连接到电脑上。然后借助 ADB 实现对手机的调控,包括点触、滑动、截图等等功能。

2、 让电脑能够看懂文字,必然需要对手机屏幕截图,然后对截图中的文字识别即可

3、 让电脑操控浏览器搜题,用 python 的库即可实现

了解了整体思路后,下面开始我们的实验。

用 Python 实现手机自动答题,这下百万答题游戏谁也玩不过我!

实验前的准备

首先我们使用的python版本是3.6.5所用到的库有os,Python 中os模块包含普遍的操作系统功能。

如果你希望你的程序能够与平台无关的话,这个模块是尤为重要的;pillow库中Image模块是在Python PIL图像处理中常见的模块,对图像进行基础操作的功能基本都包含于此模块内;Pytesseract模块是文字识别模块;webbrowser是实现浏览器的操作;time库实现等待下一题之间的间隔时间等待。

用 Python 实现手机自动答题,这下百万答题游戏谁也玩不过我!

自动获取手机文字

1 、利用 ADB 控制手机:

ADB是安卓手机常见的调控插件,我们需要将手机数据线和电脑连接,然后借助adb实现对手机的截屏并保存在指定路径。Adb控制指令如下:

截取屏幕,图片命名为screen.png :

<code>os.system(

"adb shell /system/bin/screencap -p /sdcard/screen.png"

) /<code>

将截图保存到电脑,路径为:C:/Users/jia/Desktop

<code>os.system(

"adb pull /sdcard/screen.png C:/Users/jia/Desktop"

) /<code>

模拟点击屏幕,x y分别为点击处的像素

<code>

os

.system

("

adb

shell

input

tap

{}{}"

.format

(

x

,

y

))

#x

y

为点击处的像素点/<code>

详细代码如下:

<code> 

def

get_image

()

:

os.system(

'adb shell screencap -p /sdcard/image.png'

) os.system(

'adb pull /sdcard/image.png .'

) xigua_size = (

150

,

530

,

1800

,

800

)/<code>

2 、对图片文字识别:

文字识别部分为了方便快速,我们直接使用pytesseract文字识别即可。其中主要用的函数是pytesseract.image_to_strin。

pytesser里包含了tesseract.exe和英语的数据包(默认只识别英文),还有一些示例图片,所以解压缩后即可使用。

同时模块需要PIL库的支持。

如何识别率低的问题?

可以增强图片的显示效果,或者将其转换为黑白的,这样可以使其识别率提升不少。

识别其他语言?

tesseract是一个命令行下运行的程序,参数如下:

<code>

tesseract

imagename

outbase

[-l lang]

[-psm N]

[configfile...]

/<code>

imagename是输入的image的名字,outbase是输出的文本的名字,默认为outbase.txt ,-l lang 是定义要识别的的语言,默认为英文。

详细代码如下:

<code> 
get_image()
img=Image.open('image.png')
img_que = img.crop(xigua_size)
 
question=pytesseract.image_to_string(img_que,lang='chi_sim')/<code>

获取文字后,对文字做一些处理,去除没必要的信息。

<code>

question

=question.replace(

' '

,

''

).replace(

'\n'

,

''

)

que

= question[question.find(

'.'

)+

1

: question.find(

'?'

)]/<code>
用 Python 实现手机自动答题,这下百万答题游戏谁也玩不过我!

自动搜题的实现

1 、浏览器操作:

在识别问题的基础上,我们通过 webbrowser 打开浏览器,并操作浏览器搜索答案。

其中主要用到的方法如下:

  • webbrowser 有以下方法:
<code>

webbrowser

.open

(

url

[, new=0[, autoraise=1]

])/<code>

这个方法是在默认的浏览器中显示url, 如果new = 0, 那么url会在同一个浏览器窗口下打开,如果new = 1, 会打开一个新的窗口,如果new = 2, 会打开一个新的tab, 如果autoraise = true, 窗口会自动增长。

<code>

webbrowser

.open_new

(

url

)/<code>

在默认浏览器中打开一个新的窗口来显示 url, 否则,在仅有的浏览器窗口中打开 url 。

<code>

webbrowser

.open_new_tab

(

url

)/<code>

在默认浏览器中当开一个新的 tab 来显示 url, 否则跟 open_new() 一样、

<code>

webbrowser

.get

(

[name]

)/<code>

根据 name 返回一个浏览器对象,如果 name 为空,则返回默认的浏览器

<code>

webbrowser

.register

(

name

,

construtor

[, instance]

)/<code>

注册一个名字为 name 的浏览器,如果这个浏览器类型被注册就可以用 get() 方法来获取。

详细代码如下:

<code>#引擎搜索
url = 

"https://www.baidu.com/s?wd="

+que webbrowser.

open

(url) get_image() img=Image.

open

(

'image.png'

) img_que = img.crop(xigua_size) question1= pytesseract.image_to_string(img_que,) question1=question1.replace(

' '

,

''

).replace(

'\n'

,

''

) que = question1[question.

find

(

'.'

)+

1

: question.

find

(

'?'

)]

while

True:

while

(question1==question): get_image() img = Image.

open

(

'image.png'

) img_que = img.crop(xigua_size) question1 = pytesseract.image_to_string(img_que,) question1 = question1.replace(

' '

,

''

).replace(

'\n'

,

''

) que = question1[question.

find

(

'.'

) +

1

: question.

find

(

'?'

)] get_image() img = Image.

open

(

'image.png'

) img_que = img.crop(xigua_size) # 识别截图文字 question = pytesseract.image_to_string(img_que,) question = question.replace(

' '

,

''

).replace(

'\n'

,

''

) que = question[question.

find

(

'.'

) +

1

: question.

find

(

'?'

)] continue

while

(question1!=question): get_image() img = Image.

open

(

'image.png'

) img_que = img.crop(xigua_size) # 识别截图文字 question = pytesseract.image_to_string(img_que,) question1 = pytesseract.image_to_string(img_que,) question1 = question1.replace(

' '

,

''

).replace(

'\n'

,

''

) que = question1[question.

find

(

'.'

) +

1

: question.

find

(

'?'

)] question = question.replace(

' '

,

''

).replace(

'\n'

,

''

) que = question[question.

find

(

'.'

) +

1

: question.

find

(

'?'

)] # 引擎搜索 url =

"https://www.baidu.com/s?wd="

+ que webbrowser.

open

(url) continue/<code>

到这里,我们整体的程序就搭建完成,下面为我们程序的运行结果:

用 Python 实现手机自动答题,这下百万答题游戏谁也玩不过我!

试试用这个方法参加《百万答题》类小游戏,或许下一个百万获奖人就是你。

详细源代码获取私信小编01


分享到:


相關文章: