(1)儲存整張登錄頁面的圖片
(2)剪下驗證碼區域的圖片
(3)呼叫showapiRequest識別剪下好的圖片
driver.save_screenshot(」D:\web_selenium1\image1.png「)
left = element_code.location['x']
top = element_code.location['y']
right = element_code.size['width'] + left
height = element_code.size['height'] + top
im = Image.open(」code_image/image1.png「) #絕對路徑
img = im.crop((left,top,right,height)) #注意是元組
img.save("D:\web_selenium1\image2.png")
from selenium import webdriver
from ShowapiRequest import ShowapiRequest
from PIL import Image
# python3.6.5
# 需要引入requests包 :執行終端->進入python/Scripts ->輸入:pip install requests
r = ShowapiRequest("http://route.showapi.com/184-4","323291","866123e8786b41dd9b9f4ef2d8da1c88" )
r.addFilePara("image", "code_image/image2.png")
r.addBodyPara("typeId", "14")
r.addBodyPara("convert_to_jpg", "0")
r.addBodyPara("needMorePrecise", "0")
res = r.post()
text = res.json()["showapi_res_body"]["Result"]
print(text)
當你把上面的思路弄懂,敲出程式碼後,發現crop剪下不了完整的二維條碼
原因:你電腦的解析度是150%
-解決辦法1:
-解決辦法2:
driver.save_screenshot("code_image/image1.png")
left = element_code.location['x'] * 1.5
top = element_code.location['y'] * 1.5
right = element_code.size['width'] * 1.5 + left
height = element_code.size['height'] * 1.5 + top
im = Image.open("code_image/image1.png")
img = im.crop((left,top,right,height))
img.save("code_image/image2.png")