404 Eror API Call Pexels Python

让人想犯罪 __ 提交于 2021-02-05 07:25:26

问题


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)

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)

But get 404 Error as a result. Any idea why?


回答1:


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}'


来源:https://stackoverflow.com/questions/65333807/404-eror-api-call-pexels-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!