mirror of
https://github.com/LibraHp/GetQzonehistory.git
synced 2025-02-22 21:29:06 +00:00
添加错误处理
This commit is contained in:
parent
9f5a6cc5d1
commit
87d663e121
53
main.py
53
main.py
@ -4,25 +4,48 @@ import util.RequestUtil as Request
|
||||
import util.ToolsUtil as Tools
|
||||
import util.ConfigUtil as Config
|
||||
import pandas as pd
|
||||
import signal
|
||||
|
||||
|
||||
# 信号处理函数
|
||||
def signal_handler(signal, frame):
|
||||
# 在手动结束程序时保存已有的数据
|
||||
if len(texts) > 0:
|
||||
save_data()
|
||||
exit(0)
|
||||
|
||||
|
||||
def save_data():
|
||||
df = pd.DataFrame(texts, columns=['内容'])
|
||||
df.to_excel(Config.result_path + Request.uin + '.xlsx', index=False)
|
||||
print('导出成功,请查看 ' + Config.result_path + Request.uin + '.xlsx')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
user_info = Request.get_login_user_info()
|
||||
user_nickname = user_info[Request.uin][6]
|
||||
print(f"用户<{Request.uin}>,<{user_nickname}>登录成功")
|
||||
texts = []
|
||||
for i in trange(1000, desc='Progress', unit='iteration'):
|
||||
message = Request.get_message(i * 100, 100).content.decode('utf-8')
|
||||
html = Tools.process_old_html(message)
|
||||
if "li" not in html:
|
||||
break
|
||||
soup = BeautifulSoup(html, 'html.parser')
|
||||
for element in soup.find_all('p', class_='txt-box-title ellipsis-one'):
|
||||
text = element.get_text().replace('\xa0', ' ')
|
||||
if text not in texts:
|
||||
texts.append(text)
|
||||
# 创建一个DataFrame对象
|
||||
df = pd.DataFrame(texts, columns=['内容'])
|
||||
|
||||
# 导出为Excel
|
||||
df.to_excel(Config.result_path + Request.uin + '.xlsx', index=False)
|
||||
print('导出成功,请查看 ' + Config.result_path + Request.uin + '.xlsx')
|
||||
try:
|
||||
# 注册信号处理函数
|
||||
signal.signal(signal.SIGINT, signal_handler)
|
||||
signal.signal(signal.SIGTERM, signal_handler)
|
||||
|
||||
for i in trange(1000, desc='Progress', unit='iteration'):
|
||||
message = Request.get_message(i * 100, 100).content.decode('utf-8')
|
||||
html = Tools.process_old_html(message)
|
||||
if "li" not in html:
|
||||
break
|
||||
soup = BeautifulSoup(html, 'html.parser')
|
||||
for element in soup.find_all('p', class_='txt-box-title ellipsis-one'):
|
||||
text = element.get_text().replace('\xa0', ' ')
|
||||
if text not in texts:
|
||||
texts.append(text)
|
||||
|
||||
if len(texts) > 0:
|
||||
save_data()
|
||||
except Exception as e:
|
||||
print(f"发生异常: {str(e)}")
|
||||
if len(texts) > 0:
|
||||
save_data()
|
||||
|
Loading…
Reference in New Issue
Block a user