使用Python簡單地去輔助百萬答題

2021-09-12 01:38:00 字數 3007 閱讀 7750

前段時間地跳一跳助手很有意思,使用facebook-wda去做一下自動化地小功能還是很有意思地。

思路

facebook-wda能截圖,那把答題區域裁剪,想辦法識別其中地文字,提取出來,搜尋一下。試了一下tesseract,效果不是很好,就用次數免費地api了,直接上**。

c = wda.client('')

s = c.session()

print(s.window_size())

def printnowdatetime():

print(datetime.datetime.now().strftime("%y-%m-%d %h:%m:%s"))

def getimage(url):

with open(url,'rb') as fp:

return fp.read()

def ocrimage(image):

# image = getimage('/users/user/desktop/testp.png')

""" 如果有可選引數 "

"" options = {}

options["language_type"] = "chn_eng"

options["detect_direction"] = "true"

options["detect_language"] = "true"

options["probability"] = "true"

""" 帶引數呼叫通用文字識別, 引數為本地 "

"" response = client.basicgeneral(image, options)

print(response)

print(type(response))

words = response['words_result']

for item in words:

def cvcutimg(x,y,width,height,img):

return img[y:y+height, x:x+width]

def cvbytes_to_numpyndarray(imgbytes):

img = np.asarray(bytearray(imgbytes), np.uint8)

img = cv2.imdecode(img, cv2.imread_color)

# cv2.imshow('mm', img)

# cv2.waitkey(0)

# img type is numpy.ndarray

# img = cv2.imread('/users/user/desktop/testp.png')

return img

def cvnumpyndarray_to_bytes(img):

return np.ndarray.tobytes(img)

def chongdingdahui():

img = c.screenshot('screen01.png')

# img = getimage('chongdingdahui.png')

image = cvbytes_to_numpyndarray(img)

cutimg = cvcutimg(25, 320, 700, 175, image)

cv2.imwrite('cut.png', cutimg)

image = getimage('cut.png')

ocrwd = ocrimage(image)

image = getimage('cut.png')

ocrwd = ocrimage(image)

wd = parse.quote(ocrwd)

url = '' + wd

webbrowser.open(url)

def xiguashiping():

img = c.screenshot('screen01.png')

# img = getimage('xiguaishipin.png')

image = cvbytes_to_numpyndarray(img)

cutimg = cvcutimg(40, 220, 670, 175, image)

cv2.imwrite('cut.png', cutimg)

image = getimage('cut.png')

ocrwd = ocrimage(image)

image = getimage('cut.png')

ocrwd = ocrimage(image)

wd = parse.quote(ocrwd)

url = '' + wd

webbrowser.open(url)

def zhishichaoren():

img = c.screenshot('screen01.png')

# img = getimage('zhishichaoren.png')

image = cvbytes_to_numpyndarray(img)

cutimg = cvcutimg(40, 200, 670, 200, image)

cv2.imwrite('cut.png', cutimg)

image = getimage('cut.png')

ocrwd = ocrimage(image)

image = getimage('cut.png')

ocrwd = ocrimage(image)

wd = parse.quote(ocrwd)

url = '' + wd

webbrowser.open(url)

if __name__ == "__main__":

while true:

#time.sleep(0.5)

printnowdatetime()

# chongdingdahui()

xiguashiping()

# zhishichaoren()

複製**

Python 利用get方法簡單地獲取百度首頁

實現思路 1 安裝requests庫 2 確定url 3 使用requests的get請求方法response requests.get 4 檢視響應的內容response.content response.text response.headers response.status code實現 i...

vuex及其輔助函式簡單使用

1 新建store資料夾,內部新建index.js 檔案內部內容如下 寫法一import vuex from vuex import vue from vue vue.use vuex 第一種寫法 const state const getters const actions val account...

python爬去糗事百科

1.用requests beautifulsoup抓取糗事百科的文字內容 2.將抓取的內容寫入txt。1.獲取網頁源 def get html url 用requests庫得到網頁源 html requests.get url text return html 2.檢視源 結構找到要抓取的目標 3....