简体   繁体   中英

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

I have a basic code from the Imgur library in Python; basically it's just a gallery search asking for 'Dog' pics in the Imgur gallery but I only want one result; the first one but I can't do it because when I use indexing to do that I get an error.

TypeError: 'Gallery_album' object does not support indexing

This is the code I have; it's just the basic code

imgur = pyimgur.Imgur(client_id, client_secret)

items = imgur.search_gallery('dog')

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

I'm wondering if that's the way the API behaves or I can do something else to just get one result? As default I get like 20 results and I don't want that. Thanks for any help.

Looking at gallery_search method ( https://github.com/Imgur/imgurpython/blob/master/imgurpython/client.py ), it seems to return the search result as a list for a given page (which contains that x number of result of GalleryImage/GalleryAlbum).

So it seems you have to post-process that list to retrieve a single link for a GalleryImage.

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

if res:
    link = res[0].link 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM