python自动答题脚本程序

python自动答题脚本程序


import requests
import os
from PIL import Image
from aip import AipOcr

#手机截图.图片预处理
def get_screenshot():
    # 截屏
    os.system('adb shell screencap -p /sdcard/image.png')
    os.system('adb pull /sdcard/image.png')

#文字识别
def get_word_by_img():
    # 文字识别
    APP_ID = '16227766'
    API_KEY = 'FgubvnxtReF32vR4jGsS4FY4'
    SECRET_KEY = 'R5YbhWyY2NMF102wRZTwVm9U4hjeAwQG'
    client = AipOcr(APP_ID, API_KEY, SECRET_KEY)
    i = open(r'D:\dati\new_img_fb.png', 'rb')
    img = i.read()
    img_res = client.basicGeneral(img)
    return img_res

#百度:使用requests进行访问
def baidu(question, answers):
    # 进行百度
    url = 'https://www.baidu.com/s'

    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36'
    }
    data = {
        'wd': question
    }
    res = requests.get(url=url, params=data, headers=headers)
    res.encoding = 'utf-8'
    html = res.text
    for i in range(len(answers)):
        answers[i] = (html.count(answers[i]), answers[i], i)
    answers.sort(reverse=True)
    return answers

def run():
    while True:
        input("回车答题")
        get_screenshot()
        img = Image.open('D:\dati\image.png')
        title_img = img.crop((80, 500, 1000, 880))
        answers_img = img.crop((80, 960, 1000, 1720))
        new_img = Image.new('RGBA', (920, 1140))
        new_img.paste(title_img, (0, 0, 920, 380))
        new_img.paste(answers_img, (0, 380, 920, 1140))
        new_img.save('new_img_fb.png')

#找答案
        info = get_word_by_img()
        answers = [x['words'] for x in info['words_result'][-4:]]
        question = ''.join([x['words'] for x in info['words_result'][:-4]])
        resy = baidu(question, answers)
        print(resy)

if __name__ == '__main__':
    run()


作者:码猩

物联沃分享整理
物联沃-IOTWORD物联网 » python自动答题脚本程序

发表回复