【问题标题】:404 Eror API Call Pexels Python404 错误 API 调用 Pexels Python
【发布时间】:2021-03-27 17:40:24
【问题描述】:

我想使用 Python 下载带有 Pexels API (documentation) 的图像。首先,我通过以下方式获取图片的 ID:

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 并运行它:

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 错误。知道为什么吗?

【问题讨论】:

    标签: python python-3.x api python-requests


    【解决方案1】:

    id 是 URL 路径的一部分,而不是查询字符串。请参阅文档 (https://api.pexels.com/v1/photos/2014422) 中的示例。您应该将其附加到 URL,例如:

    photo_request_link = f'https://api.pexels.com/v1/photos/{id}'
    

    【讨论】:

      猜你喜欢
      • 2012-06-19
      • 1970-01-01
      • 2017-03-06
      • 1970-01-01
      • 2015-04-10
      • 2021-05-04
      • 1970-01-01
      • 2017-06-25
      • 2021-03-20
      相关资源
      最近更新 更多