From 9bb89c99243106af3a229fb3a38665c547a1ea4a Mon Sep 17 00:00:00 2001 From: zlaazlaa <2889827787@qq.com> Date: Wed, 13 Nov 2024 18:37:12 +0800 Subject: [PATCH] Fix no attribute 'encoding' error and add fake-useragent Signed-off-by: zlaazlaa <2889827787@qq.com> --- main.py | 15 ++++++++------- requirements.txt | 3 ++- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/main.py b/main.py index 3c5c14c..a7037cc 100644 --- a/main.py +++ b/main.py @@ -17,7 +17,9 @@ import traceback import dateparser import hashlib import copy +from fake_useragent import UserAgent +ua = UserAgent() # 程序版本 version = "1.0.2" @@ -66,7 +68,7 @@ headers = { 'sec-fetch-user': '?1', 'upgrade-insecure-requests': '1', # Temporarily fix waf issues - 'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.1 Safari/605.1.15', + 'user-agent': ua.random, } @@ -1050,9 +1052,9 @@ def main(page: ft.Page): def get_message(start, count): while True: - message_content = get_message_unsafe(start, count) - if message_content.encoding.lower() == 'utf-8': - return message_content + mc = get_message_unsafe(start, count) + if isinstance(mc, requests.Response) and mc.status_code == 200 and mc.encoding.lower() == 'utf-8': + return mc def get_message_unsafe(start, count): cookies = page.session.get("user_cookies") @@ -1086,10 +1088,9 @@ def main(page: ft.Page): headers=headers, timeout=(5, 10) # 设置连接超时为5秒,读取超时为10秒 ) + return response except Exception as e: - return 'None' - - return response + return None def get_message_count(): total = 10 diff --git a/requirements.txt b/requirements.txt index 2736ab3..3230303 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,4 +3,5 @@ flet==0.24.1 pandas==2.2.3 Requests==2.31.0 openpyxl==3.1.5 -dateparser==1.2.0 \ No newline at end of file +dateparser==1.2.0 +fake-useragent \ No newline at end of file