ChatGPT大家应该都玩过吧,今天我给大家提供一个将聊天记录导出的教学。
- 打开聊天记录界面,调出控制台(F12或Fn+F12)
- 点击查看器选项卡
- 右键html标记,选择编辑为html(如下图)
- 全选复制到一个文本文件里
import requests
from lxml import etree
# 直接拷贝页面保存为文件后获取
# path为保存的html文件的路径
def getHtml(path):
with open(path, 'r', encoding='utf-8') as f:
html = f.read()
return html
def getusertalk():
html = getHtml('聊天记录.html')
Selector = etree.HTML(html)
user = Selector.xpath(
'/html/body/div[1]/div[2]/div[1]/main/div[1]/div/div/div/div/div/div[2]/div[1]/div/text()')
list1 = []
for i in user:
if i.strip() == '':
pass
else:
list1.append(i.strip())
return list1
def getgpttalk():
html = getHtml('聊天记录.html')
Selector = etree.HTML(html)
chatgpt = Selector.xpath(
'/html/body/div[1]/div[2]/div[1]/main/div[1]/div/div/div/div/div/div[2]/div[1]/div/div')
list1 = []
# pre = chatgpt[0].xpath('./pre/div/div[2]/code/span/text()')
# for i in pre:
# print(i)
# print(pre) 代码块太繁琐了,略过
for i in chatgpt:
try:
pre = i.xpath('./pre/string()')
print(pre)
except Exception:
pass
p = i.xpath('./p/text()')
if len(p) > 1:
temp = ''
for i in p:
temp += i.strip()
else:
temp = p[0].strip()
list1.append(temp)
return list1
user = getusertalk()
# print(len(user))
chatgpt = getgpttalk()
# print(len(chatgpt))
# 输出到控制台
# for i, j in zip(user, chatgpt):
# print('爱吃熊掌的鱼:' + i + '
')
# print('>ChatGPT3.5:' + j + '\n')
# 写入到文件
with open('聊天记录.md', 'w+', encoding='utf-8') as f:
for i, j in zip(user, chatgpt):
f.writelines('爱吃熊掌的鱼:'
+ i + '')
f.writelines('>ChatGPT3.5:' + j + '\n')
print(f'写入完成,共{len(user)}组问答')
导出后的聊天记录:
我与ChatGPT的对话
Chat聊天记录聊天记录导出
Chat聊天记录聊天记录导出
Chat聊天记录聊天记录导出
Chat聊天记录聊天记录导出
Chat聊天记录聊天记录导出
Chat聊天记录聊天记录导出
Chat聊天记录聊天记录导出
Chat聊天记录聊天记录导出
Chat聊天记录聊天记录导出
Chat聊天记录聊天记录导出
Chat聊天记录聊天记录导出