我试图使用 Shorte.st api 使用我的 python 程序自动创建我的短链接,但我真的不知道如何使用 API! 在专用页面中只有这段代码:
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"}
在public-api-token中,我显然必须插入我的私有(private) token ,但由于curl适用于c(我认为),我如何将它们与python一起使用? 非常感谢
最佳答案
我更喜欢使用名为 requests ( http://docs.python-requests.org/en/latest/ ) 的 python 库来处理 http 请求。您所要做的就是发送一个您想要缩短为数据字典的网址,并在“public-api-token”键下的 header 中发送您的公共(public) api token 。您可以在 https://shorte.st/tools/api 上找到您的 api token 页。响应内容是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'}
关于python - Shorte.st Api python ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25958513/