Unsplash.com:如何通过Unsplash API从预选照片ID获取用户名

2024-04-29 04:50:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个手动选择的Unsplash照片ID列表,我想在网站上显示这些ID。现在我想确保通过Unsplash API打印出正确的属性

PyUnsplash中的.get_属性函数非常适合我的用例。但是,它需要用户名或搜索查询来返回属性

我本想用photoid简单地查询API,但遗憾的是,这不起作用。 因此,如果有任何关于如何通过API从照片ID到用户名的建议,我很乐意听取

_

备选问题标题: 按照片id获取用户的个人资料


Tags: 函数apiid列表get属性网站手动
2条回答

我刚刚用Paw对一个photos/:id端点进行了一些非闪烁查询的故障排除。使用请求库,我能够从返回的json对象中提取用户名:

import requests


def send_request():
    # Request
    # GET https://api.unsplash.com/photos/4Fjjyhg1YFc

    try:
        response = requests.get(
            url="https://api.unsplash.com/photos/4Fjjyhg1YFc",
            params={
                "client_id": "YOUR_API_KEY",
            },
            headers={
                "Cookie": "",
            },
        )

        data = response.json()
        print('Response HTTP Status Code: {status_code}'.format(
            status_code=response.status_code))
        print('Unsplash API Username: {uname}'.format(
            uname=data['user']['username']))
        print('Unsplash API Full Name: {fname}'.format(
            fname=data['user']['name']))
        print('Unsplash API Location: {location}'.format(
            location=data['user']['location']))
        print('Unsplash API Portfolio: {portfolio}'.format(
            portfolio=data['user']['portfolio_url']))
        print('Unsplash API Instagram: {ig}'.format(
            ig=data['user']['instagram_username']))
    except requests.exceptions.RequestException:
        print('HTTP Request failed')

send_request()

有关更多信息,请签出文档中Get A Photo下的“响应”部分。它显示返回的所有字段,您可能会找到更多要使用的字段

github.com/salvoventura/发布了PyUnsplash的新版本。现在这是可能的

PyUnsplash v1.0.0rc1 (release candidate, v1)

This is the first release candidate for production ready 1.0.0 of PyUnsplash. Thanks to all the early adopters, there have been a number of improvements and bugfixes and now should be a good time to start the rc process.

This release brings one useful addition:

Introduction of SinglePhoto which allows instantiating a Photo object using a photo_id Deprecated format value 'str' in favor of 'txt' in get_attribution()

相关问题 更多 >