您的位置:首页 > 其它

2018-1-10 Program For Million Heroes

2018-01-10 09:12 225 查看
##Version 1.1.1
##Time:2018-1-10
##Author:十三月
import subprocess
from PIL import Image
import pytesseract
import requests
from bs4 import BeautifulSoup
import time
###得到屏幕截图
def pull_screenshot():
process = subprocess.Popen('adb shell screencap -p', shell=True, stdout=subprocess.PIPE)
screenshot = process.stdout.read()
binary_screenshot = screenshot.replace(b'\r\n', b'\n')
f = open('screenpic.png', 'wb')
f.write(binary_screenshot)
f.close()
###识别图中文字
def getImagecode():
image = Image.open('screenpic.png')
box = (50, 300, 1000, 600)
cutimage = image.crop(box)
box2 = (50, 620, 1000, 1220)
cutimage2 = image.crop(box2)
code = pytesseract.image_to_string(cutimage, lang='chi_sim').strip()
code2 = pytesseract.image_to_string(cutimage2, lang='chi_sim').strip()
codelist = [code, code2]
print(code)
print(code2)
return codelist
###解析百度页面
def getpageindex(keyword):
params = {
"Cookie": "",##此处填入浏览器中的cookie
"Host": "www.baidu.com",
"Upgrade-Insecure-Requests": '1',
"User-Agent": "",##此处填入浏览器中的User-Agent
}
url = "https://www.baidu.com/s?cl=3&wd={}".format(keyword)
res = requests.get(url, headers=params)
soup = BeautifulSoup(res.text, 'lxml')
answers = soup.select('.c-abstract')
return answers
###红色显示答案文字
def distinguish(str1, str2):
for x1 in str1:
if x1 in str2:
print("\033[0;31m%s\033[0m" % x1, end='')
else:
print(x1, end='')

def main():
inputstate = input('Run program input [0]:')
start = time.clock()
if inputstate == '0':
pull_screenshot()
[keyword, answord] = getImagecode()
answers = getpageindex(keyword)
print('--' * 10)
for i in range(len(answers)):
try:
answer = answers[i].text.strip()
print('[%d]' % i, end='')
distinguish(answer, answord)
print('\n')
except:
pass
else:
pass
end = time.clock()
print('Running time: %s Seconds' % (end-start))

if __name__ == '__main__':
main()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: