python - 您如何在 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 像素的图像?
解决方案
您的代码运行良好,但没有为所有图像指定品种名称。正如你在下面看到的,当我尝试你的“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 响应中有错误。但是,它们确实具有高度和宽度,因此如果存在此查询的端点,则应该可以请求特定的大小。否则,您可以在获得图像后自己对它们进行排序
推荐阅读
- java - 递归查找并返回数组中的最小值和最大值
- python - 如何找到单个 x 和 y 值的拟合参数
- ios - 如何使用 nativeScript 制作聊天应用程序?
- javascript - 如何在 carousal 中设置过渡效果(react)
- python-3.x - 为什么在 Python 的这个递归函数中,即使满足基本条件,执行也会继续?
- vb.net - 如何从动态创建的文本框中将数据保存到数据库中
- kotlin - 仅在创建期间生成数据类属性值
- oracle-apex - 使用 Owa_cookie 包设置 cookie
- python - 根据列条件向 pandas 数据框添加行
- python - 复制并组合两个数据框熊猫