WebUI自動化驗證碼登入如何實現?
點選檢視程式碼
# encoding=utf8
import sys
import requests
import muggle_ocr
import json
class loginerp():
def __init__(self):
# 將request 開啟會話
self.session=requests.session()
# 登入獲取cookie
def yanzhengma(self):
img_code_url = "http://192.168.1.235/system/index.php/admin/managers/imagenumber?1662719683" # 驗證碼圖片地址
png = self.session.get(url=img_code_url) # 請求驗證碼
# 將驗證碼儲存本地
filename = "D:\pycharmproject\驗證碼識別\cap.png" # 定義一個圖片地址
file = open(filename, "wb") # 以二進位制開啟一個檔案
file.write(png.content) # 寫入二進位制檔案
file.close() # 關閉檔案
# 呼叫muggle_ocr
sdk = muggle_ocr.SDK(model_type=muggle_ocr.ModelType.Captcha)
with open(r"D:\pycharmproject\驗證碼識別\cap.png", "rb") as f: # 開啟檔案
img = f.read() # 讀取圖片檔案
text = sdk.predict(image_bytes=img) # 識別驗證碼
print(text) # 列印下驗證碼
return text
def erp_login(self):
while True:
text=self.yanzhengma()
# 定義請求登入地址
url="http://192.168.1.235/system/index.php/admin/managers/login"
#定義請求頭
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36"
}
#請求請求登入的引數
data={
"username":"wengzhenxiong",
"password":"wenzhenxiong12",
"number":text, #取值為muggle_ocr 識別的驗證碼數字
"action":"login"
}
aa=self.session.post(url=url,headers=headers,data=data).text
if "首頁" in aa: #判斷是否有首頁 有則登入成功
print(111)
return aa
def get_cookiejar(self):
self.erp_login()
cookies_jar=self.session.cookies
print(cookies_jar)
# cookie = requests.utils.dict_from_cookiejar(cookie_jar)
cookies=cookies_jar.get_dict()
print(cookies)
cookie=cookies['PHPSESSID']
print(cookie)
return cookie