1.注册一个薄荷图床的的账号,并添加客服申请api测试权限
查看api文档
我们只需关注两个必选参数即可,如图中红色标注
2.编写pthon代码
其他语言也可以,只要发送post请求即可,注意url、token和照片需要更改为你自己的
import requests
def python_demo():
url = 'https://s1.bib0.com/test3/api/upload/'
files = {'uploadedFile': ('./x.jpg', open('./x.jpg', 'rb'), "image/jpeg")}
data = {'api_token': 'fe0d5e9c9e848a3ccb47',
'upload_format': 'file', # 可选值 file 、base64 或者 url,不填则默认为file
}
res = requests.post(url, data=data, files=files)
print(res.text)
json_data = json.loads(res.text)
normol_url = json_data['url'].replace('\\/', '/')
return normol_url
3.测试
运行该程序后,我们会收到回包,内容如下
{"status":"success","statusCode":"200","resultData":"success:File uploaded","uploadFormat":"file","protocolType":"https","url":"https:\/\/s1.bib0.com\/test3\/i\/2024\/09\/11\/2u93.jpg","originFileName":"x.jpg","lessurl":"s1.bib0.com\/test3\/i\/2024\/09\/11\/2u93.jpg","bbsurl":"[img]https:\/\/s1.bib0.com\/test3\/i\/2024\/09\/11\/2u93.jpg[\/img]","markdownurl":"![](https:\/\/s1.bib0.com\/test3\/i\/2024\/09\/11\/2u93.jpg)","htmlurl":"<img src='https:\/\/s1.bib0.com\/test3\/i\/2024\/09\/11\/2u93.jpg' \/>","fileNameBody":"x","fileNameExt":"jpg","fileSize":2355537,"image_x":1737,"image_y":3088,"image_pixels":5363856,"image_bits":8}
我们随便找一个在线解析json工具进行解析,结果如下
{
"status": "success",
"statusCode": "200",
"resultData": "success:File uploaded",
"uploadFormat": "file",
"protocolType": "https",
"url": "https:\/\/s1.bib0.com\/test3\/i\/2024\/09\/11\/2u93.jpg",
"originFileName": "x.jpg",
"lessurl": "s1.bib0.com\/test3\/i\/2024\/09\/11\/2u93.jpg",
"bbsurl": "[img]https:\/\/s1.bib0.com\/test3\/i\/2024\/09\/11\/2u93.jpg[\/img]",
"markdownurl": "![](https:\/\/s1.bib0.com\/test3\/i\/2024\/09\/11\/2u93.jpg)",
"htmlurl": "<img src='https:\/\/s1.bib0.com\/test3\/i\/2024\/09\/11\/2u93.jpg' \/>",
"fileNameBody": "x",
"fileNameExt": "jpg",
"fileSize": 2355537,
"image_x": 1737,
"image_y": 3088,
"image_pixels": 5363856,
"image_bits": 8
}
然后用代码提取所需的照片url即可。
标签:实战,test3,url,image,jpg,图床,bib0,com From: https://www.cnblogs.com/dwinternet/p/18408548