本文介绍了使用 Tweepy 将推文的媒体、扩展链接、引用附加到推文文本中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

今天早上穿过牛津郡车道,为快速测试做好准备…… https://t.co/W0uFKU9jCr

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

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

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

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

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

推荐答案

您说得对,您需要使用实体.您可以像这样获得 expand_url :

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)

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

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)

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

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.

这篇关于使用 Tweepy 将推文的媒体、扩展链接、引用附加到推文文本中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 16:50