90 lines
3.4 KiB
Python
90 lines
3.4 KiB
Python
import time
|
|
|
|
import requests
|
|
import hashlib
|
|
from urllib.parse import quote
|
|
|
|
from typing import Optional, Dict, AnyStr
|
|
from PIL import Image, ImageEnhance
|
|
import pytesseract
|
|
|
|
|
|
class F6_module:
|
|
@staticmethod
|
|
def get_captcha() -> AnyStr:
|
|
captcha_url = 'https://yunxiu.f6car.cn/kzf6/login/captcha-image' # 替换为实际的验证码图片URL
|
|
response = requests.get(captcha_url)
|
|
with open('captcha.png', 'wb') as f:
|
|
f.write(response.content)
|
|
|
|
image = Image.open('captcha.png')
|
|
enhancer = ImageEnhance.Contrast(image)
|
|
image = enhancer.enhance(2.0)
|
|
enhancer = ImageEnhance.Brightness(image)
|
|
image = enhancer.enhance(1.5)
|
|
image = image.convert('L')
|
|
image = image.point(lambda x: 0 if x < 128 else 255, '1')
|
|
image.save('preprocessed_captcha.png')
|
|
|
|
# 使用pytesseract识别验证码
|
|
captcha_text = pytesseract.image_to_string(image)
|
|
print(f"识别的验证码为: {captcha_text}")
|
|
|
|
return captcha_text
|
|
|
|
def login_in(self, username, password, store_name='默认门店'): # 登录模块
|
|
url = "https://yunxiu.f6car.com/kzf6/login/confirm"
|
|
session = requests.Session() # 使用 Session 对象保持会话状态
|
|
header = {
|
|
'Referer': url,
|
|
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36 Edg/130.0.0.0'
|
|
}
|
|
data = {
|
|
'username': username,
|
|
'password': hashlib.md5(password.encode('utf-8')).hexdigest(),
|
|
"mac": ""
|
|
}
|
|
|
|
try:
|
|
res = session.post(url=url, headers=header, data=data)
|
|
res_json = res.json()
|
|
print(session.cookies.items())
|
|
|
|
if res_json.get('message') == '请获取图形验证码':
|
|
|
|
time.sleep(2)
|
|
captcha_text = F6_module.get_captcha()
|
|
captcha_text = captcha_text.split('\n')[0]
|
|
data.update({'imageCode': captcha_text})
|
|
print(data)
|
|
res = session.post(url=url, headers=header, data=data)
|
|
res_json = res.json()
|
|
|
|
|
|
if res_json['data'] == None:
|
|
return res
|
|
else:
|
|
try:
|
|
res_json = res.json()
|
|
if res_json.get("data"):
|
|
for group in res_json.get('data', []):
|
|
if group["groupName"] == store_name:
|
|
groupId = group.get("groupId")
|
|
|
|
token = res_json['token']
|
|
token = quote(token) # url 编码
|
|
# token = 'b9rzRQki%2Bs%2BBmJtrtG7j%2BEXF7VnGhHKDYzV0UHLnchlJo00k36OvJA%3D%3D'# 返回error可以尝试更改token
|
|
url = f'https://yunxiu.f6car.cn/kzf6/user/loginAfterChooseGroup?token={token}&groupId={groupId}&macAddress=' # 登录分组url
|
|
res1 = session.get(url, cookies=res.cookies) # 使用正确的 cookies 获取方式
|
|
return res1
|
|
except Exception as e:
|
|
print(f"Error during login_in: {e}")
|
|
return None
|
|
except Exception as e:
|
|
print(f"Error during login: {e}")
|
|
return None
|
|
|
|
@staticmethod
|
|
def dict_to_cookie_string(cookies_dict):
|
|
return '; '.join([f"{key}={value}" for key, value in cookies_dict.items()])
|