404 错误 API 调用 Pexels Python [英] 404 Eror API Call Pexels Python
本文介绍了404 错误 API 调用 Pexels Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用 Python 下载带有 Pexels API(documentation)的图像.首先,我通过执行以下操作获取图片的 ID:
I want to download an image with Pexels API (documentation) with Python. First, I get the ID of the picture by doing:
import requests
image_base_url = 'https://api.pexels.com/v1/search'
api_key = 'my_api_key'
my_obj = {'query':'Stock market'}
x = requests.get(image_base_url,headers = {'Authorization':api_key},data = my_obj)
print(x.text)
然后,我获取我想要的图像的 ID 并运行:
Then, I obtain an ID for the image I want and run this:
photo_request_link = 'https://api.pexels.com/v1/photos/'
photo_id = {'id':159888}
final_photo = requests.get(photo_request_link,headers = {'Authorization':api_key},data=photo_id)
print(final_photo)
但结果是 404 错误.知道为什么吗?
But get 404 Error as a result. Any idea why?
推荐答案
id 是 URL 路径的一部分,而不是查询字符串.请参阅文档中的示例 (https://api.pexels.com/v1/photos/2014422).您应该将其附加到 URL,例如:
The id is a part of the URL path, not the querystring. See the example in the documentation (https://api.pexels.com/v1/photos/2014422). You should append it to the URL, for example:
photo_request_link = f'https://api.pexels.com/v1/photos/{id}'
这篇关于404 错误 API 调用 Pexels Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文