Python - 在 Imgur API 中仅显示一定数量的查询结果

Python - Display only a certain amount of results from a query in the Imgur API

我在 Python 中有来自 Imgur 库的基本代码;基本上它只是一个图库搜索,要求在 Imgur 图库中搜索 'Dog' 图片,但我只想要一个结果;第一个,但我做不到,因为当我使用索引来做到这一点时,我得到了一个错误。

TypeError: 'Gallery_album' object does not support indexing

这是我的代码;这只是基本代码

imgur = pyimgur.Imgur(client_id, client_secret)

items = imgur.search_gallery('dog')

for item in items:
    print(item[0].link)

我想知道 API 的行为方式是否如此,或者我可以做其他事情来获得一个结果?默认情况下,我会得到 20 个结果,但我不希望这样。感谢您的帮助。

查看gallery_search方法(https://github.com/Imgur/imgurpython/blob/master/imgurpython/client.py ),它似乎 return 搜索结果作为给定页面的列表(其中包含 GalleryImage/GalleryAlbum 的 x 个结果)。

因此您似乎必须 post 处理该列表以检索 GalleryImage 的单个 link。

res = [ item for item in  imgur.search_gallery('dog') if isinstance(item, GalleryImage) ]

if res:
    link = res[0].link