【问题标题】:How do I print out the full url with tweepy?如何使用 tweepy 打印出完整的 url?
【发布时间】:2012-08-08 16:07:26
【问题描述】:

如何在 tweepy(而不是 t.co 链接)中打印出完整的 url?以下代码打印出“这是一个测试链接 http://t.co/93Hme7Jv90210”,即使 twitter.com 显示“这是一个测试链接 http://www.test.com/test90210”。

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

【问题讨论】:

    标签: python tweepy


    【解决方案1】:

    不确定如何使用 tweepy,但您想将 include_entities 设置为 True,并且 Twitter API 将包含带有响应的 t.co URL 的完整 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' object has no attribute 'urls'
    猜你喜欢
    • 2022-06-21
    • 1970-01-01
    • 2011-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-27
    • 2017-06-29
    • 2020-04-01
    相关资源
    最近更新 更多