diff --git a/main.py b/main.py index dc76c48..619f011 100644 --- a/main.py +++ b/main.py @@ -30,13 +30,13 @@ if __name__ == '__main__': print(f"登录失败:请重新登录,错误信息:{str(e)}") exit(0) texts = [] - + count = Request.get_message_count() try: # 注册信号处理函数 signal.signal(signal.SIGINT, signal_handler) signal.signal(signal.SIGTERM, signal_handler) - for i in trange(1000, desc='Progress', unit='100条'): + for i in trange(int(count / 100) + 1, desc='Progress', unit='100条'): message = Request.get_message(i * 100, 100).content.decode('utf-8') html = Tools.process_old_html(message) if "li" not in html: @@ -50,8 +50,8 @@ if __name__ == '__main__': if time_element is not None and text_element is not None: time = time_element.get_text().replace('\xa0', ' ') text = text_element.get_text().replace('\xa0', ' ') - if text not in [sublist[1] for sublist in texts] and time is not None and text is not None: - texts.append([time, text]) + if text not in [sublist[1] for sublist in texts]: + texts.append([time, text]) if len(texts) > 0: save_data() diff --git a/util/RequestUtil.py b/util/RequestUtil.py index 6530699..332e11a 100644 --- a/util/RequestUtil.py +++ b/util/RequestUtil.py @@ -1,8 +1,9 @@ import re - +from tqdm import tqdm import util.LoginUtil as Login import requests import json + # 登陆后获取到的cookies cookies = Login.cookie() # 获取g_tk @@ -63,3 +64,22 @@ def get_login_user_info(): info = info.strip().lstrip('portraitCallBack(').rstrip(');') info = json.loads(info) return info + + +def get_message_count(): + # 初始的总量范围 + lower_bound = 0 + upper_bound = 100000 # 假设最大总量为1000000 + total = upper_bound // 2 # 初始的总量为上下界的中间值 + with tqdm(desc="正在获取消息列表数量...") as pbar: + while lower_bound <= upper_bound: + response = get_message(total, 100) + if "li" in response.text: + # 请求成功,总量应该在当前总量的右侧 + lower_bound = total + 1 + else: + # 请求失败,总量应该在当前总量的左侧 + upper_bound = total - 1 + total = (lower_bound + upper_bound) // 2 # 更新总量为新的中间值 + pbar.update(1) + return total