图床简介
sm.ms网站提供免费的图床服务。单图上传大小限制5MB,每次最多上传10张,支持多种图片链接格式和api接口调用。
获取令牌
- 注册账号并登录,点击User-Dashboard
- 点击API Token,点击Generate Secret Token
- 记住或复制你的令牌,上传图片的时候会用到
cMZkqPKLXfroLwwGBqpYtunwkKm6BvUp
测试样例
安装依赖
pip3 install requests
上传图片
- 将图片路径作为参数传给upload函数,如我这里的
anime.png
- 将
Authorization
后面的字符串改为你之前获取的令牌字符串
import requests
import json
def upload(path):
headers = {'Authorization': 'cMZkqPKLXfroLwwGBqpYtunwkKm6BvUp'}
files = {'smfile': open(path, 'rb')}
url = 'https://sm.ms/api/v2/upload'
res = requests.post(url, files=files, headers=headers).json()
print(json.dumps(res, indent=4))
if __name__ == "__main__":
upload('anime.png')
结果输出
可以看到服务器成功的响应了我们的上传请求,并返回了url链接https://i.loli.net/2020/09/16/FWzobx3PcRhGEJ7.png
{
"success": true,
"code": "success",
"message": "Upload success.",
"data": {
"file_id": 0,
"width": 1600,
"height": 900,
"filename": "anime.png",
"storename": "FWzobx3PcRhGEJ7.png",
"size": 1441300,
"path": "/2020/09/16/FWzobx3PcRhGEJ7.png",
"hash": "AH8BbaWrcwZEPROqpFkC56h1mu",
"url": "https://i.loli.net/2020/09/16/FWzobx3PcRhGEJ7.png",
"delete": "https://sm.ms/delete/AH8BbaWrcwZEPROqpFkC56h1mu",
"page": "https://sm.ms/image/FWzobx3PcRhGEJ7"
},
"RequestId": "DB695918-3624-4026-AE82-2AE70B630708"
}
温馨提示
如果你只想输出url链接,请将代码中的print(json.dumps(res, indent=4))
改为print(res['data']['url'])
引用参考
sm.ms图床api文档完整版python实现:python-sm.ms-api