Append tweet's media, expanded links, quotes to the tweet text using Tweepy

◇◆丶佛笑我妖孽 提交于 2021-02-20 04:26:07

问题


For instance, with my current code, this tweet shows as:

Stunning ride through the Oxfordshire lanes this morning to get the legs ready for the Fast Test… https:// t.co/W0uFKU9jCr

I want to look like as it shown on Twitter's website, e.g.:

Stunning ride through the Oxfordshire lanes this morning to get the legs ready for the Fast Test… https://www.instagram.com/p/BSocl5Djf5v/

How can I do this exactly? I mean replacing Twitter's short urls by the urls of media, expanded urls, tweet quotes... I know it has to do with the 'entities' object in the json but I'm not sure how to handle that in my code

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

回答1:


You are right that you need to use entities. You can get the expanded_url like so:

 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)

You can make this print out the the status text and the expanded_url by concatenating them

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)

Not that this code will only print when the tweet has URLs, so I believe it will not print a tweet if no media link is shared.



来源:https://stackoverflow.com/questions/43298113/append-tweets-media-expanded-links-quotes-to-the-tweet-text-using-tweepy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!