Skip to content

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

1 Commit

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 

Repository files navigation

Doubao API

一个使用 Python Playwright 与豆包(Doubao)进行交互的库,支持通过配置文件自定义浏览器设置和选择器。

功能特性

  • 🚀 使用系统浏览器:自动检测并使用系统中已安装的浏览器(Edge、Firefox、Chrome等)
  • ⚙️ 配置灵活:通过配置文件或代码参数进行详细配置
  • 📁 缓存支持:使用浏览器缓存目录,提升后续访问速度
  • 🔄 登录状态管理:自动检测登录状态,支持持久化登录
  • 💬 交互式对话:支持发送消息并获取豆包的回复
  • 📋 上下文管理器:支持使用 with 语句自动管理资源
  • 🔍 智能选择器:内置多种选择器,适配不同版本的豆包界面

安装方法

1. 克隆仓库

git clone https://github.com/under-the-ocean/doubao-api.git
cd doubao-api

2. 安装依赖

pip install playwright
playwright install

使用示例

基本用法

from doubao_api import DoubaoAPI

# 创建实例
doubao = DoubaoAPI()

# 启动浏览器
if doubao.start():
    # 发送消息
    response = doubao.send_message("你好,豆包")
    print(f"回复: {response}")
    
    # 发送第二条消息
    response = doubao.send_message("你能做什么")
    print(f"回复: {response}")

# 停止浏览器
doubao.stop()

使用上下文管理器

from doubao_api import DoubaoAPI

# 使用上下文管理器
with DoubaoAPI() as doubao:
    # 发送消息
    response = doubao.send_message("什么是Python")
    print(f"回复: {response}")
# 上下文管理器会自动调用stop()

自定义配置

from doubao_api import DoubaoAPI

# 自定义配置
doubao = DoubaoAPI(
    browser={
        "type": "edge",  # 浏览器类型:edge, chrome, firefox, webkit
        "headless": False,  # 是否无头模式
        "width": 1920,  # 浏览器窗口宽度
        "height": 1080,  # 浏览器窗口高度
        "path": "",  # 浏览器可执行文件路径(自动检测)
        "cache_dir": "D:\BrowserCache"  # 缓存目录
    }
)

try:
    if doubao.start():
        response = doubao.send_message("如何学习Python")
        print(f"回复: {response}")
finally:
    doubao.stop()

配置文件

创建 config.json 文件来自定义配置:

{
    "browser": {
        "type": "edge",
        "headless": false,
        "width": 1920,
        "height": 1080,
        "path": "",
        "cache_dir": ""
    },
    "selectors": {
        "input": [
            "textarea",
            "input[type='text']",
            "[data-testid='chat-input']",
            ".chat-input"
        ],
        "send": [
            "button:has-text('发送')",
            "button:has-text('Send')",
            "[data-testid='send-button']",
            ".send-button",
            "button[type='submit']"
        ],
        "reply": [
            "[data-testid='message_text_content']",
            ".reply-content",
            ".message-content",
            "[data-testid='message-content']",
            ".chat-message"
        ],
        "new_chat": [
            "button:has-text('新对话')",
            "button:has-text('New Chat')",
            "[data-testid='new-chat-button']",
            ".new-chat-button"
        ]
    }
}

然后使用配置文件:

from doubao_api import DoubaoAPI

# 使用配置文件
doubao = DoubaoAPI(config_path="config.json")
# ...

注意事项

  1. 首次使用需要登录:第一次运行时,会自动打开浏览器并提示您登录豆包
  2. 登录状态持久化:登录状态会通过缓存目录持久化,后续运行无需重新登录
  3. 浏览器路径:如果自动检测失败,可以在配置文件中手动指定浏览器路径
  4. 选择器适配:如果豆包界面更新导致选择器失效,可以在配置文件中更新选择器
  5. 网络环境:需要良好的网络环境以确保豆包能够正常访问
  6. Playwright版本:建议使用最新版本的Playwright以获得最佳体验

常见问题

1. 浏览器启动失败

  • 检查浏览器是否已安装
  • 尝试手动指定浏览器路径
  • 确保Playwright已正确安装:playwright install

2. 无法获取回复内容

  • 检查网络连接是否正常
  • 尝试更新配置文件中的选择器
  • 确保豆包界面已完全加载

3. 登录后仍然提示未登录

  • 检查缓存目录是否可写
  • 尝试清除缓存目录后重新登录
  • 确保登录操作已完成(点击登录按钮后等待页面跳转)

贡献指南

欢迎提交Issue和Pull Request来改进这个项目!

  1. Fork 本仓库
  2. 创建特性分支:git checkout -b feature/amazing-feature
  3. 提交更改:git commit -m 'Add some amazing feature'
  4. 推送到分支:git push origin feature/amazing-feature
  5. 打开Pull Request

许可证

本项目采用 MIT 许可证 - 详见 LICENSE 文件

免责声明

本项目仅用于学习和研究目的,使用时请遵守豆包的使用条款和相关法律法规。


享受与豆包的智能交互! 🤖✨

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages