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

2024-09-28 22:31:48 发布

您现在位置:Python中文网/ 问答频道 /正文

例如,对于我当前的代码,this tweet显示为:

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

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

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

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

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

Tags: theto代码textforstatusthistweet
1条回答
网友
1楼 · 发布于 2024-09-28 22:31:48

你是对的,你需要使用实体。您可以获得如下扩展的\u 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)

你可以通过连接url来打印

^{pr2}$

并不是说这个代码只在tweet有url时才会打印,所以我相信如果没有共享媒体链接,它就不会打印tweet。在

相关问题 更多 >