import requests import pyautogui from PIL import Image from pyzbar.pyzbar import decode # 登录请求的URL url = 'https://bbs.mihoyo.com/apihub/api/user/auth' # 请求头信息 headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36', 'Referer': 'https://bbs.mihoyo.com/', 'X-Requested-With': 'XMLHttpRequest', 'Content-Type': 'application/json;charset=UTF-8'} # 截取屏幕并保存为图片 screenshot = pyautogui.screenshot() screenshot.save('screenshot.png') # 加载图片并解析二维码 image = Image.open('screenshot.png') decoded = decode(image) # 输出二维码内容 if decoded: print(decoded[0].data.decode('utf-8')) else: print('未找到二维码') # 账号和密码 username = '18110615228' password = 'password' # 构造请求体 data = { 'account': username, 'password': password, 'autoLogin': False} # 发送登录请求 response = requests.post(url, json=data, headers=headers) # 判断登录是否成功 if response.status_code == 200: print('登录成功') else: print('登录失败')
时间: 2024-01-27 14:04:25 浏览: 148
可以将截屏并解析二维码的代码封装成一个函数,代码如下:
```python
import requests
import pyautogui
from PIL import Image
from pyzbar.pyzbar import decode
def decode_qr_code():
# 截取屏幕并保存为图片
screenshot = pyautogui.screenshot()
screenshot.save('screenshot.png')
# 加载图片并解析二维码
image = Image.open('screenshot.png')
decoded = decode(image)
# 输出二维码内容
if decoded:
return decoded[0].data.decode('utf-8')
else:
return None
```
然后,在主函数中调用该函数获取二维码信息,代码如下:
```python
# 登录请求的URL
url = 'https://bbs.mihoyo.com/apihub/api/user/auth'
# 请求头信息
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36',
'Referer': 'https://bbs.mihoyo.com/',
'X-Requested-With': 'XMLHttpRequest',
'Content-Type': 'application/json;charset=UTF-8'
}
# 账号和密码
username = '18110615228'
password = 'password'
# 构造请求体
data = {
'account': username,
'password': password,
'autoLogin': False
}
# 获取二维码信息
qr_code = decode_qr_code()
if qr_code:
# 将二维码信息添加到请求体中
data['qrCode'] = qr_code
# 发送登录请求
response = requests.post(url, json=data, headers=headers)
# 判断登录是否成功
if response.status_code == 200:
print('登录成功')
else:
print('登录失败')
else:
print('未找到二维码')
```
这样就能够大大提高代码的可读性和维护性。
阅读全文