首页 > 解决方案 > 您如何在 The Cat API 中按品种召唤猫?

问题描述

我正在使用 Python3 调用The Cat API

def get_cat_by_breed(breed_name):
    from requests import get
    from json import loads
    url = "https://api.thecatapi.com/v1/images/search"
    breeds = [{"name": breed_name}]
    params = {"breeds": breeds,
              "api_key": api_key,
              "limit": 100,
              "page": 1}
    response = get(
        url=url,
        params=params)
    return loads(response.content)

这成功地得到了一只猫,但不是指定的breed_name。我究竟做错了什么?

另外,如何搜索 >=x 像素和 >=y 像素的图像?

标签: python

解决方案


您的代码运行良好,但没有为所有图像指定品种名称。正如你在下面看到的,当我尝试你的“abys”代码时

[{'breeds': [], 'categories': [{'id': 5, 'name': 'boxes'}], 'id': 'hn', 'url': 'https://cdn2.thecatapi.com/images/hn.jpg', 'width': 1200, 'height': 1600}, {'breeds': [], 'id': '182', 'url': 'https://cdn2.thecatapi.com/images/182.jpg', 'width': 448, 'height': 336}]

第一个结果没有品种名称或 ID ......所以我猜 API 响应中有错误。但是,它们确实具有高度和宽度,因此如果存在此查询的端点,则应该可以请求特定的大小。否则,您可以在获得图像后自己对它们进行排序


推荐阅读