404错误API调用Pexels Python [英] 404 Eror API Call Pexels Python

查看:65
本文介绍了404错误API调用Pexels Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过Python使用Pexels API(文档)下载图像.首先,我通过执行以下操作获得图片的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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆