2024-02-13 10:22:44 +00:00
|
|
|
|
from bs4 import BeautifulSoup
|
|
|
|
|
from tqdm import trange
|
2024-02-13 05:45:57 +00:00
|
|
|
|
import util.RequestUtil as Request
|
2024-02-13 10:22:44 +00:00
|
|
|
|
import util.ToolsUtil as Tools
|
2024-02-13 12:47:18 +00:00
|
|
|
|
import util.ConfigUtil as Config
|
2024-02-13 10:22:44 +00:00
|
|
|
|
import pandas as pd
|
2024-02-13 13:15:37 +00:00
|
|
|
|
import signal
|
2024-09-11 01:46:59 +00:00
|
|
|
|
import os
|
|
|
|
|
import re
|
|
|
|
|
from tqdm import trange,tqdm
|
|
|
|
|
import requests
|
|
|
|
|
import time
|
2024-09-11 07:07:51 +00:00
|
|
|
|
import platform
|
2024-02-13 13:15:37 +00:00
|
|
|
|
|
|
|
|
|
# 信号处理函数
|
|
|
|
|
def signal_handler(signal, frame):
|
|
|
|
|
# 在手动结束程序时保存已有的数据
|
|
|
|
|
if len(texts) > 0:
|
|
|
|
|
save_data()
|
|
|
|
|
exit(0)
|
|
|
|
|
|
|
|
|
|
|
2024-09-11 01:46:59 +00:00
|
|
|
|
|
2024-02-13 13:15:37 +00:00
|
|
|
|
def save_data():
|
2024-02-14 11:26:39 +00:00
|
|
|
|
df = pd.DataFrame(texts, columns=['时间', '内容'])
|
2024-02-13 13:15:37 +00:00
|
|
|
|
df.to_excel(Config.result_path + Request.uin + '.xlsx', index=False)
|
|
|
|
|
print('导出成功,请查看 ' + Config.result_path + Request.uin + '.xlsx')
|
|
|
|
|
|
2024-09-11 01:46:59 +00:00
|
|
|
|
def save_data():
|
|
|
|
|
user_save_path = Config.result_path + Request.uin + '/'
|
|
|
|
|
pic_save_path = user_save_path + 'pic/'
|
|
|
|
|
if not os.path.exists(user_save_path):
|
|
|
|
|
os.makedirs(user_save_path)
|
|
|
|
|
print(f"Created directory: {user_save_path}")
|
|
|
|
|
if not os.path.exists(pic_save_path):
|
|
|
|
|
os.makedirs(pic_save_path)
|
|
|
|
|
print(f"Created directory: {pic_save_path}")
|
|
|
|
|
pd.DataFrame(texts, columns=['时间', '内容', '图片链接']).to_excel(user_save_path + Request.uin + '_全部列表.xlsx', index=False)
|
|
|
|
|
pd.DataFrame(all_friends, columns=['昵称', 'QQ', '空间主页']).to_excel(user_save_path + Request.uin + '_好友列表.xlsx', index=False)
|
|
|
|
|
for item in tqdm(texts, desc="处理消息列表", unit="item"):
|
|
|
|
|
item_text = item[1]
|
|
|
|
|
item_pic_link = item[2]
|
|
|
|
|
if item_pic_link is not None and len(item_pic_link) > 0 and 'http' in item_pic_link:
|
|
|
|
|
# 保存图片
|
|
|
|
|
pic_name = re.sub(r'[\\/:*?"<>|]', '_', item_text) + '.jpg'
|
|
|
|
|
# 去除文件名中的空格
|
|
|
|
|
pic_name = pic_name.replace(' ', '')
|
|
|
|
|
|
|
|
|
|
# 限制文件名长度
|
|
|
|
|
if len(pic_name) > 40:
|
|
|
|
|
pic_name = pic_name[:40] + '.jpg'
|
|
|
|
|
# pic_name = pic_name.split(':')[1] + '.jpg'
|
|
|
|
|
response = requests.get(item_pic_link)
|
|
|
|
|
if response.status_code == 200:
|
|
|
|
|
with open(pic_save_path + pic_name, 'wb') as f:
|
|
|
|
|
f.write(response.content)
|
|
|
|
|
if user_nickname in item_text:
|
|
|
|
|
if '留言' in item_text:
|
|
|
|
|
leave_message.append(item)
|
|
|
|
|
elif '转发' in item_text:
|
|
|
|
|
forward_message.append(item)
|
|
|
|
|
else:
|
|
|
|
|
user_message.append(item)
|
|
|
|
|
else:
|
|
|
|
|
other_message.append(item)
|
|
|
|
|
pd.DataFrame(user_message, columns=['时间', '内容', '图片链接']).to_excel(user_save_path + Request.uin + '_说说列表.xlsx', index=False)
|
|
|
|
|
pd.DataFrame(forward_message, columns=['时间', '内容', '图片链接']).to_excel(user_save_path + Request.uin + '_转发列表.xlsx', index=False)
|
|
|
|
|
pd.DataFrame(leave_message, columns=['时间', '内容', '图片链接']).to_excel(user_save_path + Request.uin + '_留言列表.xlsx', index=False)
|
|
|
|
|
pd.DataFrame(other_message, columns=['时间', '内容', '图片链接']).to_excel(user_save_path + Request.uin + '_其他列表.xlsx', index=False)
|
|
|
|
|
print('\033[36m' + '导出成功,请查看 ' + user_save_path + Request.uin + ' 文件夹内容' + '\033[0m')
|
|
|
|
|
print('\033[32m' + '共有 ' + str(len(texts)) + ' 条消息' + '\033[0m')
|
|
|
|
|
print('\033[36m' + '最早的一条说说发布在' + texts[texts.__len__() - 1][0] + '\033[0m')
|
|
|
|
|
print('\033[32m' + '好友列表共有 ' + str(len(all_friends)) + ' 个好友' + '\033[0m')
|
|
|
|
|
print('\033[36m' + '说说列表共有 ' + str(len(user_message)) + ' 条说说' + '\033[0m')
|
|
|
|
|
print('\033[32m' + '转发列表共有 ' + str(len(forward_message)) + ' 条转发' + '\033[0m')
|
|
|
|
|
print('\033[36m' + '留言列表共有 ' + str(len(leave_message)) + ' 条留言' + '\033[0m')
|
|
|
|
|
print('\033[32m' + '其他列表共有 ' + str(len(other_message)) + ' 条内容' + '\033[0m')
|
|
|
|
|
print('\033[36m' + '图片列表共有 ' + str(len(os.listdir(pic_save_path))) + ' 张图片' + '\033[0m')
|
2024-09-11 07:07:51 +00:00
|
|
|
|
|
|
|
|
|
if platform.system() == "Windows":
|
|
|
|
|
current_directory = os.getcwd()
|
|
|
|
|
os.startfile(current_directory + user_save_path[1:])
|
|
|
|
|
os.system('pause')
|
|
|
|
|
else:
|
|
|
|
|
# 兼容Linux
|
|
|
|
|
full_path = os.path.abspath(user_save_path)
|
|
|
|
|
print("数据已经写入"+ full_path)
|
|
|
|
|
|
|
|
|
|
|
2024-02-13 12:47:18 +00:00
|
|
|
|
|
2024-02-12 09:29:14 +00:00
|
|
|
|
if __name__ == '__main__':
|
2024-02-14 01:12:18 +00:00
|
|
|
|
try:
|
|
|
|
|
user_info = Request.get_login_user_info()
|
|
|
|
|
user_nickname = user_info[Request.uin][6]
|
|
|
|
|
print(f"用户<{Request.uin}>,<{user_nickname}>登录成功")
|
|
|
|
|
except Exception as e:
|
|
|
|
|
print(f"登录失败:请重新登录,错误信息:{str(e)}")
|
|
|
|
|
exit(0)
|
2024-02-13 10:22:44 +00:00
|
|
|
|
texts = []
|
2024-09-11 01:46:59 +00:00
|
|
|
|
all_friends = []
|
|
|
|
|
other_message = []
|
|
|
|
|
user_message = []
|
|
|
|
|
leave_message = []
|
|
|
|
|
forward_message = []
|
2024-02-14 13:15:17 +00:00
|
|
|
|
count = Request.get_message_count()
|
2024-02-13 13:15:37 +00:00
|
|
|
|
try:
|
|
|
|
|
# 注册信号处理函数
|
|
|
|
|
signal.signal(signal.SIGINT, signal_handler)
|
|
|
|
|
signal.signal(signal.SIGTERM, signal_handler)
|
|
|
|
|
|
2024-02-14 13:15:17 +00:00
|
|
|
|
for i in trange(int(count / 100) + 1, desc='Progress', unit='100条'):
|
2024-02-13 13:15:37 +00:00
|
|
|
|
message = Request.get_message(i * 100, 100).content.decode('utf-8')
|
2024-09-11 01:46:59 +00:00
|
|
|
|
time.sleep(0.2)
|
2024-02-13 13:15:37 +00:00
|
|
|
|
html = Tools.process_old_html(message)
|
|
|
|
|
if "li" not in html:
|
2024-09-10 07:54:17 +00:00
|
|
|
|
continue
|
2024-02-13 13:15:37 +00:00
|
|
|
|
soup = BeautifulSoup(html, 'html.parser')
|
2024-02-14 11:26:39 +00:00
|
|
|
|
for element in soup.find_all('li', class_='f-single f-s-s'):
|
2024-09-11 01:46:59 +00:00
|
|
|
|
put_time = None
|
2024-02-14 11:26:39 +00:00
|
|
|
|
text = None
|
2024-09-11 01:46:59 +00:00
|
|
|
|
img = None
|
|
|
|
|
friend_element = element.find('a', class_='f-name q_namecard')
|
|
|
|
|
# 获取好友昵称和QQ
|
|
|
|
|
if friend_element is not None:
|
|
|
|
|
friend_name = friend_element.get_text()
|
|
|
|
|
friend_qq = friend_element.get('link')[9:]
|
|
|
|
|
friend_link = friend_element.get('href')
|
|
|
|
|
if friend_qq not in [sublist[1] for sublist in all_friends]:
|
|
|
|
|
all_friends.append([friend_name, friend_qq, friend_link])
|
2024-02-14 11:26:39 +00:00
|
|
|
|
time_element = element.find('div', class_='info-detail')
|
|
|
|
|
text_element = element.find('p', class_='txt-box-title ellipsis-one')
|
2024-09-11 01:46:59 +00:00
|
|
|
|
img_element = element.find('a', class_='img-item')
|
2024-02-14 11:26:39 +00:00
|
|
|
|
if time_element is not None and text_element is not None:
|
2024-09-11 01:46:59 +00:00
|
|
|
|
put_time = time_element.get_text().replace('\xa0', ' ')
|
2024-02-14 11:26:39 +00:00
|
|
|
|
text = text_element.get_text().replace('\xa0', ' ')
|
2024-09-11 01:46:59 +00:00
|
|
|
|
if img_element is not None:
|
|
|
|
|
img = img_element.find('img').get('src')
|
2024-02-14 13:15:17 +00:00
|
|
|
|
if text not in [sublist[1] for sublist in texts]:
|
2024-09-11 01:46:59 +00:00
|
|
|
|
texts.append([put_time, text, img])
|
2024-02-13 13:15:37 +00:00
|
|
|
|
|
|
|
|
|
if len(texts) > 0:
|
|
|
|
|
save_data()
|
|
|
|
|
except Exception as e:
|
|
|
|
|
print(f"发生异常: {str(e)}")
|
|
|
|
|
if len(texts) > 0:
|
2024-09-11 01:46:59 +00:00
|
|
|
|
save_data()
|