教你用python撸走《百万英雄》《冲顶大会》奖金。

栏目: Python · 发布时间: 6年前

内容简介:教你用python撸走《百万英雄》《冲顶大会》奖金。

最近这类答题app比较火,我的同事 wangtonghe 为开源社区贡献了他的 python 代码。以下文章为他的思路,我只做了部分整理发布于掘金社区,分享给大家。

  • 起因

看了 《程序员如何玩转《冲顶大会》?》 大受启发,不过弱点很多,需要使用付费的OCR接口、再open到百度搜索答案,我们等待加载并且寻找答案的时候,已经错失了好的机会,刚好前几天研究了下微信跳一跳的辅助,正好可以用上。

-初步思路

思路很明确,把答案截图pull过来,通过PYTHON OCR 库进行识别成文字后再放到百度搜索。匹配出现率最频繁的词语,记过几番尝试后,一些容易搜索的问题还是是可以搜索大部分答案的。

  • 尝试

目前它是手动的,也就是说每次答案出现,手动执行脚本返回答案。同样由于个别题目原因(如某个词有多少笔画)虽然不是百分之百的成功率,但是一般都能进入决赛+一张复活卡基本妥妥‘吃鸡’,下面是吃鸡截图:

教你用python撸走《百万英雄》《冲顶大会》奖金。
教你用python撸走《百万英雄》《冲顶大会》奖金。
教你用python撸走《百万英雄》《冲顶大会》奖金。
  • 技术栈

实现语言python,用到的类库如下:

  1. PIL
  2. pytesseract(图片识别库)
  3. BeautifulSoup(页面解析)

文字识别引擎需单独安装,参见 Python人工智能之图片识别,Python3一行代码实现图片文字识别 以及 mac上文字识别 Tesseract-OCR for mac

主体代码如下:

import os
from PIL import Image
import pytesseract
from urllib.request import urlopen
import urllib.request
from bs4 import BeautifulSoup

DEFAULT_WIDTH = 720
DEFAULT_HEIGHT = 1280


def main():
    # 720*1280分辨率坐标
    left_top_x = 30
    left_top_y = 200
    right_bottom_x = 680
    right_bottom_y = 380

    # 1. 截图
    os.system('adb shell screencap -p /sdcard/answer.png')
    os.system('adb pull /sdcard/answer.png answer.png')

    # 2. 截取题目并文字识别
    image = Image.open('answer.png')
    crop_img = image.crop((left_top_x, left_top_y, right_bottom_x, right_bottom_y))
    crop_img.save('crop.png')
    text = pytesseract.image_to_string(crop_img, lang='chi_sim')
    print(text)

    # 3. 去百度知道搜索
    text = text[2:]  # 把题号去掉
    # text = '一亩地大约是多少平米'
    wd = urllib.request.quote(text)
    url = 'https://zhidao.baidu.com/search?ct=17&pn=0&tn=ikaslist&rn=10&fr=wwwt&word={}'.format(
        wd)
    print(url)
    result = urlopen(url)
    body = BeautifulSoup(result.read(), 'html5lib')
    good_result_div = body.find(class_='list-header').find('dd')
    second_result_div = body.find(class_='list-inner').find(class_='list')
    if good_result_div is not None:
        good_result = good_result_div.get_text()
        print(good_result.strip())

    if second_result_div is not None:
        second_result = second_result_div.find('dl').find('dd').get_text()
        print(second_result.strip())


if __name__ == '__main__':
    main()

文字识别需经训练,训练越多结果越准。

我把代码放到github上了,可围观 hq-answer-assist

  • 结语

要想实现更智能化,有个思路是不停的截图(1秒一次),一旦截到答题页(可以用答题页的色差来做),做文字识别后百度,将百度后的结果与选项做比较,哪个出现次数最多哪个就是最佳答案,这里可以加个判断,如果特别确定直接模拟点击事件选答案,不确定就手工。

有同学提到分析请求,也是个思路,后续可以研究。

欢迎探讨其他更好的实现方式。


以上所述就是小编给大家介绍的《教你用python撸走《百万英雄》《冲顶大会》奖金。》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Algorithms of the Intelligent Web

Algorithms of the Intelligent Web

Haralambos Marmanis、Dmitry Babenko / Manning Publications / 2009-7-8 / GBP 28.99

Web 2.0 applications provide a rich user experience, but the parts you can't see are just as important-and impressive. They use powerful techniques to process information intelligently and offer featu......一起来看看 《Algorithms of the Intelligent Web》 这本书的介绍吧!

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码

URL 编码/解码
URL 编码/解码

URL 编码/解码

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具