想要将豆包AI中的历史对话备份到本地硬盘的 backup 目录下,以下提供几种可行的方法,从简单的手动导出到自动化脚本方案。
方法一:手动导出(最简单,无需编程)
这是官方支持的方式,安全且稳定,适合非技术用户:
- 打开豆包网页端(https://www.doubao.com)并登录
- 点击左下角用户名 → 选择「设置」→「数据管理」
- 找到「导出对话记录」选项,选择导出格式(JSON/CSV)
- 下载后手动移动到本地
backup目录
方法二:自动化脚本(Python)
以下脚本通过模拟网页操作实现自动备份(需安装必要依赖),适用于有编程基础的用户:
前置条件
bash
# 安装依赖库
pip install selenium==4.15.2 requests python-dotenv
完整备份脚本
python
# -*- coding: utf-8 -*-
import os
import json
import time
import requests
from datetime import datetime
from dotenv import load_dotenv
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
# 加载环境变量(建议将敏感信息存放在.env文件)
load_dotenv()
# 配置项
BACKUP_DIR = "./backup" # 本地备份目录
DOUBAO_COOKIE = os.getenv("DOUBAO_COOKIE") # 从浏览器获取的豆包cookie
USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36"
def init_backup_dir():
"""初始化备份目录"""
if not os.path.exists(BACKUP_DIR):
os.makedirs(BACKUP_DIR)
print(f"创建备份目录: {BACKUP_DIR}")
def get_conversations():
"""获取对话列表(核心接口)"""
headers = {
"Cookie": DOUBAO_COOKIE,
"User-Agent": USER_AGENT,
"Referer": "https://www.doubao.com",
"Content-Type": "application/json"
}
# 豆包对话列表接口(需根据实际接口调整)
url = "https://www.doubao.com/api/v1/conversations"
try:
response = requests.get(url, headers=headers, timeout=30)
response.raise_for_status()
return response.json()
except Exception as e:
print(f"获取对话列表失败: {e}")
return None
def backup_conversation(conversation_id, title):
"""备份单条对话"""
headers = {
"Cookie": DOUBAO_COOKIE,
"User-Agent": USER_AGENT
}
url = f"https://www.doubao.com/api/v1/conversations/{conversation_id}/messages"
try:
response = requests.get(url, headers=headers, timeout=30)
response.raise_for_status()
messages = response.json()
# 生成备份文件名
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
safe_title = "".join(c for c in title if c.isalnum() or c in (' ', '-', '_')).strip()
filename = f"{safe_title}_{conversation_id}_{timestamp}.json"
filepath = os.path.join(BACKUP_DIR, filename)
# 保存到本地
with open(filepath, "w", encoding="utf-8") as f:
json.dump(messages, f, ensure_ascii=False, indent=2)
print(f"成功备份: {filename}")
return True
except Exception as e:
print(f"备份对话 {conversation_id} 失败: {e}")
return False
def main():
"""主函数"""
init_backup_dir()
# 获取所有对话
conversations = get_conversations()
if not conversations:
print("未获取到对话数据")
return
# 遍历备份
success_count = 0
total_count = len(conversations)
for conv in conversations:
conv_id = conv.get("id")
conv_title = conv.get("title", f"未命名对话_{conv_id}")
if backup_conversation(conv_id, conv_title):
success_count += 1
time.sleep(1) # 避免请求过快
print(f"\n备份完成!成功: {success_count}/{total_count}")
print(f"备份文件已保存至: {os.path.abspath(BACKUP_DIR)}")
if __name__ == "__main__":
main()
使用说明
-
获取Cookie:
- 打开豆包网页端,按F12打开开发者工具
- 切换到Network标签,刷新页面
- 找到任意请求,复制Request Headers中的Cookie值
- 创建
.env文件,添加:DOUBAO_COOKIE=你的cookie值
-
运行脚本:
bashpython doubao_backup.py
方法三:Selenium全自动备份(模拟人工操作)
如果API接口有变化,可使用浏览器自动化:
python
# -*- coding: utf-8 -*-
import os
import time
from datetime import datetime
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
BACKUP_DIR = "./backup"
def init_driver():
"""初始化浏览器驱动"""
options = webdriver.ChromeOptions()
options.add_argument("--headless") # 无头模式(不显示浏览器窗口)
options.add_experimental_option("excludeSwitches", ["enable-automation"])
driver = webdriver.Chrome(options=options)
return driver
def login(driver):
"""登录豆包(需要手动扫码)"""
driver.get("https://www.doubao.com")
print("请在30秒内完成扫码登录...")
time.sleep(30)
return driver.current_url == "https://www.doubao.com/chat"
def export_conversations(driver):
"""导出对话"""
# 点击头像
avatar = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.CLASS_NAME, "avatar"))
)
avatar.click()
# 点击设置
settings = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, "//span[text()='设置']"))
)
settings.click()
# 点击导出
export_btn = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, "//button[text()='导出对话记录']"))
)
export_btn.click()
# 等待下载完成
time.sleep(10)
# 移动下载文件到backup目录
download_dir = os.path.expanduser("~/Downloads")
for file in os.listdir(download_dir):
if "豆包对话记录" in file and file.endswith((".json", ".csv")):
src = os.path.join(download_dir, file)
dst = os.path.join(BACKUP_DIR, f"{datetime.now().strftime('%Y%m%d_%H%M%S')}_{file}")
os.rename(src, dst)
print(f"备份完成: {dst}")
def main():
if not os.path.exists(BACKUP_DIR):
os.makedirs(BACKUP_DIR)
driver = init_driver()
try:
if login(driver):
export_conversations(driver)
else:
print("登录失败")
finally:
driver.quit()
if __name__ == "__main__":
main()
重要提醒
- Cookie有有效期,过期后需要重新获取
- 请勿频繁请求API,避免账号受限
- 建议将脚本加入定时任务(Windows任务计划程序/Linux crontab)实现自动备份
- 优先使用官方导出功能,脚本方案仅作学习参考
总结
- 新手推荐:使用方法一(手动导出),简单安全且无需编程基础
- 自动化需求:使用方法二(API备份),效率高但需要获取Cookie
- 兼容性优先:使用方法三(Selenium),模拟人工操作更稳定但需要安装浏览器驱动
- 所有备份文件都会保存在本地
backup目录,建议定期检查备份是否成功