class="hide-preCode-box">

方法 2:使用 urllib(Python 内置模块)

urllib 是另一个 Python 标准库,可以用于发送 HTTP 请求。

示例代码:
import urllib.request
import json

# DeepSeek API 的 URL
api_url = "https://api.deepseek.com/v1/your-endpoint"

# 你的 API 密钥
api_key = "your_api_key_here"

# 请求参数
payload = {
    "param1": "value1",
    "param2": "value2"
}

# 设置请求头
headers = {
    "Authorization": f"Bearer {api_key}",
    "Content-Type": "application/json"
}

# 创建请求对象
data = json.dumps(payload).encode("utf-8")
req = urllib.request.Request(api_url, data=data, headers=headers, method="POST")

# 发送请求
try:
    with urllib.request.urlopen(req) as response:
        response_data = response.read().decode("utf-8")
        print("API 调用成功!")
        print(json.loads(response_data))
except urllib.error.HTTPError as e:
    print(f"API 调用失败,状态码:{e.code}")
    print(e.read().decode("utf-8"))
 class="hljs-button signin active" data-title="登录复制" data-report-click="{"spm":"1001.2101.3001.4334"}"> class="hide-preCode-box">

###PS: 如果没有requests,安装 requests 库(推荐)
如果你可以安装第三方库,强烈建议使用 requests,因为它更简单、更易用。你可以通过以下命令安装:

pip install requests
 class="hljs-button signin active" data-title="登录复制" data-report-click="{"spm":"1001.2101.3001.4334"}">

安装后,可以使用 requests 调用 API,代码会更简洁:


总结

如果你有更多关于 DeepSeek API 的具体需求或问题,欢迎继续提问!

注意事项


如果你有 DeepSeek API 的具体文档或需求,可以提供更多信息,我可以帮你进一步优化代码!

data-report-view="{"mod":"1585297308_001","spm":"1001.2101.3001.6548","dest":"https://blog.csdn.net/qq_36053756/article/details/145582932","extend1":"pc","ab":"new"}">>
注:本文转载自blog.csdn.net的PlutoZuo的文章"https://blog.csdn.net/PlutoZuo/article/details/133042013"。版权归原作者所有,此博客不拥有其著作权,亦不承担相应法律责任。如有侵权,请联系我们删除。
复制链接

评论记录:

未查询到任何数据!