我试图使用shorte.st api 使用我的python 程序自动创建我的短链接,但我真的不知道如何使用Apis!在专用页面中,这里只有这个代码:
curl H "public-api-token: ---" -X -d "urlToShorten=google.com" PUT http://api.shorte.st/v1/data/url {"status":"ok","shortenedUrl":"http:\/\/sh.st\/XXXX"}
Run Code Online (Sandbox Code Playgroud)
在 public-api-token 中,我显然必须插入我的私有令牌,但是由于 curl 用于 c(我认为)我如何将它们与 python 一起使用?非常感谢
我更喜欢使用名为 requests ( http://docs.python-requests.org/en/latest/ ) 的python lib来处理 http 请求。您所要做的就是在“public-api-token”键下的标头中发送一个您想要缩短的网址作为数据字典和您的公共api令牌。您可以在https://shorte.st/tools/api页面上找到您的 api 令牌。响应内容是一个json编码的字符串,所以你需要对其进行解码以获得dict对象。
import requests
response = requests.put("https://api.shorte.st/v1/data/url", {"urlToShorten":"google.com"}, headers={"public-api-token": "your_api_token"})
print response.content
>>> {"status":"ok","shortenedUrl":"http:\\/\\/sh.st\\/ryHyU"}
import json
decoded_response = json.loads(response.content)
print decoded_response
>>>{u'status': u'ok', u'shortenedUrl': u'http://sh.st/ryHyU'}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1042 次 |
| 最近记录: |