python - API:Python中使用curl调用URL

标签 python json api url curl

我正在尝试在我的 python 脚本中使用链接缩短服务,但是它们的 API documentation是 JSON 格式(使用curl 调用 URL)。我是初学者,所以不知道如何实现。

最佳答案

鉴于所提供文档的第一个示例,您可以简单地使用 requests像这样的模块:

import requests

data = '{"username":"<Username or email>","password":"<Password>"}' #equal to: -d '{"url":"<Some URL>"}'

response = requests.get('https://www.capsulink.com/api/login', data=data)

其他一切都可以以此为基础构建。

编辑

使用 requests 请求缩短的链接:

import requests

header = {"Api-Key": "<Some API key>"} #equal to: -H 'Api-Key: <Some API key>'

data = '{"url": "<Some URL>"}' #equal to: -d '{"url":"<Some URL>"}'

response = requests.get('https://www.capsulink.com/api/capsulate', headers=header, data=data)

只需替换 "<Some API key>"使用您需要的 API key 和 "<Some URL>"以及您感兴趣的相应网址。

关于python - API:Python中使用curl调用URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58690203/

相关文章:

java - 使用 Google 的 Gson 反序列化 Bugzilla JSON 时出现问题

api - Zapier 从 Tsheets 数据在 Google Sheet 中创建多行

python - Rest 客户端的元语言

python - 在 Spark 中堆叠 ML 算法

python - python 多处理中的多个可迭代对象作为参数

mysql - 带有特殊字符的全文,用于在 json 中替换。怎么了?

python - Microsoft Bot Framework 与 python 运行时出现问题

mysql过滤json数据取值范围

api - Haskell 中来自 .NET 的 GroupBy 函数

azure - 使用函数中的值设置 AzApiManagementPolicy