使用 Tweepy 将推文的媒体、扩展链接、引用附加到推文文本中



例如,使用我当前的代码,这条推文显示为:

今天早上在牛津郡的车道上惊艳地骑行,为快速测试做好准备......https://t.co/W0uFKU9jCr

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

今天早上在牛津郡的车道上惊艳地骑行,为快速测试做好准备......https://www.instagram.com/p/BSocl5Djf5v/

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

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

你是对的,你需要使用实体。您可以像这样获得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(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时才打印,因此我相信如果没有共享媒体链接,它不会打印推文。

最新更新