11

How do I print out the full urls in tweepy (rather than the t.co link)? The following code prints out "this is a test link http://t.co/93Hme7Jv 90210", even though twitter.com shows "this is a test link http://www.test.com/test 90210".

import tweepy, random

consumer_key="my_key"
consumer_secret="my_secret"
access_token="my_access"
access_token_secret="my_token"

rand = random.randint(1,999999999)

if __name__ == '__main__':
    auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
    auth.set_access_token(access_token, access_token_secret)
    status = tweepy.API(auth)
    tweepy.API(auth).update_status('this is a test link http://www.test.com/test %s' % (rand))
    user = 'test_user'
    for status in tweepy.Cursor(status.user_timeline, id=user).items(20): 
        print status.text
user_78361084
  • 3,538
  • 22
  • 85
  • 147

2 Answers2

11

Not sure how that'd work with tweepy, but you want to set include_entities to True, and the Twitter API will include the full URLs of t.co URLs with responses.

Probably something like:

for status in tweepy.Cursor(status.user_timeline, id=user, include_entities=True).items(20): 
    for url in status.entities['urls']:
         print url['expanded_url']
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
0

it worked for me! Thank you @Martijn Pieters, (please ignore indentation error)

user = "BBC"
for status in tweepy.Cursor(api.user_timeline, id=user, 
    include_entities=True).items(20): 
        for url in status.entities['urls']:
            print (url['expanded_url'])
Asad Ali
  • 81
  • 1
  • 7