pigeonburger
pigeonburger

Reputation: 736

Python : Getting a TypeError when working with Tweepy dictionaries

I am trying to capture media urls from people's tweets using Tweepy in python.

tweet_media = clean_data['entities']['media']['media_url']

tweet_media is supposed to be the direct url to the media from a tweet, but I keep getting this error:

Exception has occurred: TypeError
list indices must be integers or slices, not str
    tweet_media = clean_data['entities']['media']['media_url']

Clean_data is where all the tweet data is stored during the stream.

It was my understanding that under entities and media, there was media_url, so what's going on here?

Thanks in advance for any help.

Upvotes: 0

Views: 100

Answers (1)

Just for fun
Just for fun

Reputation: 4225

If you see your data clean_data['entities']['media'] is a list, so you need to access its 1st element and then get its media_url

tweet_media = clean_data['entities']['media'][0]['media_url']
print(tweet_media)
>>> 'http://pbs.twimg.com/media/EgaJqy0U8AAlL7B.jpg'

Upvotes: 1

Related Questions