Search code examples
python-3.xtwitterbotstweepy

Fetch expanded_url from a twitter user using tweepy


def get_latest_tweet():
  usertweet = api.user_timeline(screen_name='user',count='1',tweetmode="extended") [0]
  print (usertweet.urls)

get_latest_tweet()

So far i've tried using "usertweet.expanded_url" instead of "usertweet.urls" but, I'm still getting the same error of

'Status' object has no attribute 'urls'

although

print(usertweet.text)

is working fine

UPDATE :- expanded url is inside entities/url section

 urls=(jypetweet.entities["urls"])
  print(urls)

I'm getting the following output:

[{'url': 'https://twit.co/PcgY0E5qM', 'expanded_url': 'https://live.tv/video/17615', 'display_url': 'live.tv/video/17615', 'indices': [107, 130]}]

But i'm still not able to access urls part


Solution

  • This was the original code :-

    def get_latest_tweet():
      jypetweet = api.user_timeline(screen_name='jypetwice',count='1',tweetmode="extended") [0]
      urls=(jypetweet.entities["urls"])
      rem_brackets= str(urls)[1:-1]
      res = ast.literal_eval(rem_brackets)
      tweet_url=res[0]['expanded_url']
      print (tweet_url)
    

    This was the second edit :-

    def get_latest_tweet():
      jypetweet = api.user_timeline(screen_name='jypetwice',count='1',tweetmode="extended") [0]
      urls=(jypetweet.entities["urls"])
      tweet_url=urls[0]["expanded_url"]
      print(tweet_url)
    

    This is the final code :-

    def get_latest_tweet():
      jypetweet = api.user_timeline(screen_name='jypetwice',count='1',tweetmode="extended") [0]
      urle=(jypetweet.entities["urls"][0]["expanded_url"])
      print (urle)