【问题标题】:Append tweet's media, expanded links, quotes to the tweet text using Tweepy使用 Tweepy 将推文的媒体、扩展链接、引号附加到推文文本
【发布时间】:2017-09-04 00:26:24
【问题描述】:

例如,使用我当前的代码,this tweet 显示为:

今早穿过牛津郡的小巷,为快速测试做好准备……https://t.co/W0uFKU9jCr

我希望看起来像 Twitter 网站上显示的那样,例如:

今早穿越牛津郡小巷,为快速测试做好准备……https://www.instagram.com/p/BSocl5Djf5v/

我该怎么做呢?我的意思是用媒体网址、扩展网址、推文引号替换 Twitter 的短网址……我知道这与 json 中的 'entities' object 有关,但我不确定如何在我的代码中处理它

for status in new_tweets:
    if ('RT @' not in status.full_text):
        id = status.id
        text = status.full_text

【问题讨论】:

    标签: python python-2.7 twitter tweepy


    【解决方案1】:

    你是对的,你需要使用实体。你可以像这样得到expand_url:

     for status in tweepy.Cursor(twitter_api.user_timeline, screenname=username).items(limit):
        if status.entities['urls']:
            for url in status.entities['urls']:
                links = url['expanded_url']
    print(links)
    

    您可以通过连接它们来打印出状态文本和expanded_url

    for status in tweepy.Cursor(twitter_api.user_timeline, screenname=username).items(limit):
        if status.entities['urls']:
            for url in status.entities['urls']:
                links = url['expanded_url']
                print(status.text + links)
    

    并不是说这段代码只会在推文有 URL 时打印,所以我相信如果没有共享媒体链接,它不会打印推文。

    【讨论】:

    • 谢谢。不过需要注意的是......我在收到一些错误后发现,检查推文是否包含网址(或媒体)的正确方法是 if 'urls' in status.entities: 而不是 if status.entities['urls']:
    猜你喜欢
    • 2020-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-10
    • 2022-06-11
    • 1970-01-01
    相关资源
    最近更新 更多