如何使用tweepy打印完整的URL?

11

如何在 tweepy 中打印完整的 URL(而不是 t.co 链接)?以下代码打印出“this is a test link http://t.co/93Hme7Jv 90210”,尽管 twitter.com 显示“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
2个回答

11

不确定如何在 tweepy 中实现,但您需要将 include_entities 设置为 True,Twitter API 将在响应中包括t.coURL的完整URL。

可能类似于:

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']

我遇到了一个错误:AttributeError: 'dict'对象没有属性'urls'。 - user_78361084

0

对我有用!谢谢@Martijn Pieters, (请忽略缩进错误)

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'])

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接