2017-06-02 25 views
0

我使用帶有名爲Twython的Python庫的Twitter API從推文中提取URL。我使用home_timeline API並查看entities來搜索URL。 通常情況下,entities中的鏈接是正確的。但是,在某些情況下,鏈接是錯誤的。例如,這是從帳戶@WWF-Philippines鳴叫: enter image description hereTweet對象的實體部分錯誤的URL

當我將光標懸停在突出顯示的鏈接,它顯示shortened_url1(我不能忍受這裏,因爲#2不允許)在狀態欄上。如果我點擊鏈接,它會打開一個外部的article。不過,如果我使用Twitter的API來查詢相應的鳴叫,這裏是我的鳴叫得到:

Need a guide to properly enjoy the great outdoors while minimizing human impact? This list is for you!\xe2\x80\xa6 shortened_url2 

你可以在這裏看到shortened_url2是從懸停光標時顯示的真正的鏈接(shortened_url1)不同。如果我按照shortened_url2,它會打開相同的推文。 entities部分的鏈接與此錯誤鏈接(shortened_url2)相同。

那麼Twitter API在這裏有什麼問題? 謝謝。

+0

如何發佈你寫的一些代碼? – elena

回答

1

我想你正在尋找舊的版本的實體。

Twitter的狀態 - https://twitter.com/WWF_Philippines/status/869027117652033536

從API調用https://api.twitter.com/1.1/statuses/show/869027117652033536.json爲我們提供了以下實體:

"truncated": true, 
"entities": { 
    "hashtags": [], 
    "symbols": [], 
    "user_mentions": [], 
    "urls": [{ 
     "url": "https:\/\/t.co\/UatUzmm9re", 
     "expanded_url": "https:\/\/twitter.com\/i\/web\/status\/869027117652033536", 
     "display_url": "twitter.com\/i\/web\/status\/8\u2026", 
     "indices": [104, 127] 
    }] 
}, 

通知,它說:"truncated": true,頂部?

最近的Twitter改變了它是如何顯示的鳴叫以及它們如何在API中表示 - 看到https://dev.twitter.com/overview/api/upcoming-changes-to-tweets

您需要添加?tweet_mode=extended到您的查詢的末尾。這會讓你回來:

"truncated": false, 
"display_text_range": [0, 126], 
"entities": { 
    "hashtags": [], 
    "symbols": [], 
    "user_mentions": [], 
    "urls": [{ 
     "url": "https:\/\/t.co\/BgKxmFzrQc", 
     "expanded_url": "http:\/\/bit.ly\/7LNTPrinciples", 
     "display_url": "bit.ly\/7LNTPrinciples", 
     "indices": [103, 126] 
    }], 

其中包含你想要的數據。

+0

謝謝,這可能是原因。但是,Twython還不支持參數'tweet_mode'。 – lenhhoxung

+0

它的確如此。請參閱https://github.com/ryanmcgrath/twython/issues/430 - 您需要添加諸如'tweet = twitter.show_status(id = tweet_id,tweet_mode ='extended')' –

相關問題