How to print all thumbnail_url from the data

Question:

I am new in Python and trying to print all thumbnail_url from the data? in Python? The data is medias

the issue is its logging only one thumbnail_url but in the data their are multiple thumbnail_urls –

But i want to print al thumbnail_url from that data how to do that with my code?

from instagrapi import Client

cl = Client()

user_id = cl.user_id_from_username("taylorswift")
medias = cl.user_medias(user_id, 2)
print(medias)

for media in medias:
    print(media.thumbnail_url)

When i print medias i logs this:

[Media(pk='3166098261500503829', id='3166098261500503829_11830955', code='CvwPZgYuCcV', taken_at=datetime.datetime(2023, 8, 10, 6, 19, 6, tzinfo=datetime.timezone.utc), media_type=1, image_versions2={}, product_type='', thumbnail_url=HttpUrl('https://instagram.flko10-1.fna.fbcdn.net/v/t51.2885-15/366422100_147674528370406_9143482148277133418_n.jpg?stp=dst-jpg_e35_s1080x1080&_nc_ht=instagram.flko10-1.fna.fbcdn.net&_nc_cat=1&_nc_ohc=cdW2KRgKhFwAX_q213Y&edm=APU89FABAAAA&ccb=7-5&oh=00_AfDN_8KdH9W8IkKQctB-YuU2PB8jTNBfiqPI3txQ3U68IA&oe=653B9F64&_nc_sid=bc0c2c', ), location=None, user=UserShort(pk='11830955', username='taylorswift', full_name='', profile_pic_url=None, profile_pic_url_hd=None, is_private=None, stories=[]), comment_count=0, comments_disabled=False, commenting_disabled_for_viewer=False, like_count=12103691, play_count=None, has_liked=None, caption_text='Surprise!! 1989 (Taylor’s Version) is on its way to you  ! The 1989 album changed my life in countless ways, and it fills me with such excitement to announce that my version of it will be out October 27th. To be perfectly honest, this is my most FAVORITE re-record I’ve ever done because the 5 From The Vault tracks are so insane. I can’t believe they were ever left behind. But not for long! Pre order 1989 (Taylor’s Version) on my site  nn : @bethgarrabrant', accessibility_caption=None, usertags=[Usertag(user=UserShort(pk='34234869', username='bethgarrabrant', full_name='', profile_pic_url=None, profile_pic_url_hd=None, is_private=None, stories=[]), x=0.1084444444, y=0.9653333333)], sponsor_tags=[], video_url=None, view_count=0, video_duration=0.0, title='', resources=[], clips_metadata={}), Media(pk='3216661270631227092', id='3216661270631227092_11830955', code='Cyj4GIOux7U', taken_at=datetime.datetime(2023, 10, 19, 0, 38, 46, tzinfo=datetime.timezone.utc), media_type=8, image_versions2={}, product_type='', thumbnail_url=None, location=None, user=UserShort(pk='11830955', username='taylorswift', full_name='', profile_pic_url=None, profile_pic_url_hd=None, is_private=None, stories=[]), comment_count=0, comments_disabled=False, commenting_disabled_for_viewer=False, like_count=8958347, play_count=None, has_liked=None, caption_text='What a truly mind blowing thing you’ve turned The Eras Tour Concert Film into. I’ve been watching videos of you guys in the theaters dancing and prancing and recreating choreography, creating inside jokes, casting spells, getting engaged, and just generally creating the exact type of joyful chaos we’re known for   One of my favorite things you’ve done was when you supported Cruel Summer SO much, I ended up starting The Eras Tour show with it. For old times sake, I’m releasing the live audio from the tour so we can all shriek it in the comfort of our homes and cars PLUS a brand new remix by @lpgiobbi   Thank you, so much, forever, wow, just thank you!!!', accessibility_caption=None, usertags=[], sponsor_tags=[], video_url=None, view_count=0, video_duration=0.0, title='', resources=[Resource(pk='3216661266730456068', video_url=None, thumbnail_url=HttpUrl('https://instagram.flko10-1.fna.fbcdn.net/v/t51.2885-15/393317010_188736784271768_181787686406934824_n.jpg?stp=dst-jpg_e35_s1080x1080&_nc_ht=instagram.flko10-1.fna.fbcdn.net&_nc_cat=1&_nc_ohc=Mf-BegeGDaEAX9Om1it&edm=APU89FABAAAA&ccb=7-5&oh=00_AfCwfa3eSMrNLadCAHXVYTI1G4S0IqbeNdBnQE8LjUqNpQ&oe=653B8EFC&_nc_sid=bc0c2c', ), media_type=1), Resource(pk='3216661266713656181', video_url=None, thumbnail_url=HttpUrl('https://instagram.flko10-1.fna.fbcdn.net/v/t51.2885-15/392900218_3509080919308926_3206171248972258297_n.jpg?stp=dst-jpg_e35_s1080x1080&_nc_ht=instagram.flko10-1.fna.fbcdn.net&_nc_cat=1&_nc_ohc=Z7G6afnlzV4AX_LBdD7&edm=APU89FABAAAA&ccb=7-5&oh=00_AfCw4dIzWnrTtSLG6WLY1Ms86H5_oSYZftA23wk8Hva5bQ&oe=653B894C&_nc_sid=bc0c2c', ), media_type=1)], clips_metadata={})]
Asked By: James

||

Answers:

That’s because your data is nested, as seen if you format your Medias object:

    [
    Media(
        pk="3166098261500503829",
        id="3166098261500503829_11830955",
        code="CvwPZgYuCcV",
        taken_at=datetime.datetime(2023, 8, 10, 6, 19, 6, tzinfo=datetime.timezone.utc),
        media_type=1,
        image_versions2={},
        product_type="",
        thumbnail_url=HttpUrl(
            "https://instagram.flko10-1.fna.fbcdn.net/v/t51.2885-15/366422100_147674528370406_9143482148277133418_n.jpg?stp=dst-jpg_e35_s1080x1080&_nc_ht=instagram.flko10-1.fna.fbcdn.net&_nc_cat=1&_nc_ohc=cdW2KRgKhFwAX_q213Y&edm=APU89FABAAAA&ccb=7-5&oh=00_AfDN_8KdH9W8IkKQctB-YuU2PB8jTNBfiqPI3txQ3U68IA&oe=653B9F64&_nc_sid=bc0c2c",
        ),
        location=None,
        user=UserShort(
            pk="11830955",
            username="taylorswift",
            full_name="",
            profile_pic_url=None,
            profile_pic_url_hd=None,
            is_private=None,
            stories=[],
        ),
        comment_count=0,
        comments_disabled=False,
        commenting_disabled_for_viewer=False,
        like_count=12103691,
        play_count=None,
        has_liked=None,
        caption_text="Surprise!! 1989 (Taylor’s Version) is on its way to you  ! The 1989 album changed my life in countless ways, and it fills me with such excitement to announce that my version of it will be out October 27th. To be perfectly honest, this is my most FAVORITE re-record I’ve ever done because the 5 From The Vault tracks are so insane. I can’t believe they were ever left behind. But not for long! Pre order 1989 (Taylor’s Version) on my site  nn : @bethgarrabrant",
        accessibility_caption=None,
        usertags=[
            Usertag(
                user=UserShort(
                    pk="34234869",
                    username="bethgarrabrant",
                    full_name="",
                    profile_pic_url=None,
                    profile_pic_url_hd=None,
                    is_private=None,
                    stories=[],
                ),
                x=0.1084444444,
                y=0.9653333333,
            )
        ],
        sponsor_tags=[],
        video_url=None,
        view_count=0,
        video_duration=0.0,
        title="",
        resources=[],
        clips_metadata={},
    ),
    Media(
        pk="3216661270631227092",
        id="3216661270631227092_11830955",
        code="Cyj4GIOux7U",
        taken_at=datetime.datetime(
            2023, 10, 19, 0, 38, 46, tzinfo=datetime.timezone.utc
        ),
        media_type=8,
        image_versions2={},
        product_type="",
        thumbnail_url=None,
        location=None,
        user=UserShort(
            pk="11830955",
            username="taylorswift",
            full_name="",
            profile_pic_url=None,
            profile_pic_url_hd=None,
            is_private=None,
            stories=[],
        ),
        comment_count=0,
        comments_disabled=False,
        commenting_disabled_for_viewer=False,
        like_count=8958347,
        play_count=None,
        has_liked=None,
        caption_text="What a truly mind blowing thing you’ve turned The Eras Tour Concert Film into. I’ve been watching videos of you guys in the theaters dancing and prancing and recreating choreography, creating inside jokes, casting spells, getting engaged, and just generally creating the exact type of joyful chaos we’re known for   One of my favorite things you’ve done was when you supported Cruel Summer SO much, I ended up starting The Eras Tour show with it. For old times sake, I’m releasing the live audio from the tour so we can all shriek it in the comfort of our homes and cars PLUS a brand new remix by @lpgiobbi   Thank you, so much, forever, wow, just thank you!!!",
        accessibility_caption=None,
        usertags=[],
        sponsor_tags=[],
        video_url=None,
        view_count=0,
        video_duration=0.0,
        title="",
        resources=[
            Resource(
                pk="3216661266730456068",
                video_url=None,
                thumbnail_url=HttpUrl(
                    "https://instagram.flko10-1.fna.fbcdn.net/v/t51.2885-15/393317010_188736784271768_181787686406934824_n.jpg?stp=dst-jpg_e35_s1080x1080&_nc_ht=instagram.flko10-1.fna.fbcdn.net&_nc_cat=1&_nc_ohc=Mf-BegeGDaEAX9Om1it&edm=APU89FABAAAA&ccb=7-5&oh=00_AfCwfa3eSMrNLadCAHXVYTI1G4S0IqbeNdBnQE8LjUqNpQ&oe=653B8EFC&_nc_sid=bc0c2c",
                ),
                media_type=1,
            ),
            Resource(
                pk="3216661266713656181",
                video_url=None,
                thumbnail_url=HttpUrl(
                    "https://instagram.flko10-1.fna.fbcdn.net/v/t51.2885-15/392900218_3509080919308926_3206171248972258297_n.jpg?stp=dst-jpg_e35_s1080x1080&_nc_ht=instagram.flko10-1.fna.fbcdn.net&_nc_cat=1&_nc_ohc=Z7G6afnlzV4AX_LBdD7&edm=APU89FABAAAA&ccb=7-5&oh=00_AfCw4dIzWnrTtSLG6WLY1Ms86H5_oSYZftA23wk8Hva5bQ&oe=653B894C&_nc_sid=bc0c2c",
                ),
                media_type=1,
            ),
        ],
        clips_metadata={},
    ),
]

The second thumbnail_url is None and the other 4 are part of a Resource object in the Resources list.

so you’ll have to add a second loop to check all the Resource objects for a thumbnail_url as well. something like the below should do the trick:

for media in medias:
    print(media.thumbnail_url)
    for resource in media.resources:
        print(resource.thumbnail_url)
Answered By: I like Bananas
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.