Jahongir Jahongir - 3 days ago 6
Python Question

Get all media with type "image" from Instagram API

I am trying to get only images of a user from Instagram API like this:

shop_recent_items, next_ = instagram_api.user_recent_media(
user_id=obj.insta_user_id,
count=33)


this returns both images and videos. I tried even this:

shop_recent_items, next_ = instagram_api.user_recent_media(
user_id=obj.insta_user_id,
type='image'
count=33)


again I get both image and video type media.

How do I get only images?

avi avi
Answer

Looking at the official documentation, it says it may return a mix of both image and video types. So you cannot possibly say you only want images. Instead, once you retrieve the info, extract images.

So once you get the JSON data, using a for loop, you can find all the images. Sample code:

for media in shop_recent_items:
    try:
        print media['images']
    except KeyEror:
        continue

If you could show me exact response and contents of shop_recent_items, I will edit this sample code accordingly.

Comments