簡體   English   中英

使用Tweepy附加推文的媒體,擴展的鏈接,對推文的引用

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

例如,在我當前的代碼中, 此推文顯示為:

今天早上穿越牛津郡的車道,令人驚嘆,為快速測試做好了准備……https:// t.co/W0uFKU9jCr

我想要看起來像Twitter網站上顯示的樣子,例如:

今天早上穿越牛津郡的車道令人驚嘆,為快速測試做好了准備…… https://www.instagram.com/p/BSocl5Djf5v/

我該怎么做呢? 我的意思是用媒體的URL,擴展的URL,tweet引號代替Twitter的短URL……我知道這與json中的“ entities”對象有關,但是我不確定如何在我的代碼中處理

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

正確,您需要使用實體。 您可以這樣獲得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)

不是說此代碼僅在tweet具有URL時才會打印,因此我相信如果沒有共享媒體鏈接,則不會打印tweet。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM