2017-04-25 57 views
0

你好,我剛剛進入Twython。我試圖阻止我的腳本嘗試多次轉推Tweet(因爲這會引發錯誤)Twython返回tweet ['retweetted']的假值

所以我想出瞭如果您有推文,retweeted鍵應該會返回,您已經轉推了推文。

但是我寫這個劇本:

def develop(twitter, keyword, count): 
    global TweetIds, tweetInsert, FollowCounts 
    results = twitter.search(q=keyword,count=count,result_type = 'popular') 
    tweets = results['statuses'] 
    for tweet in tweets: 
     if not tweet['retweeted']: 
      try: 
       twitter.retweet(id=tweet['id']) 
      except TwythonError as e: 
       print(e) 

這是輸出

Twitter API returned a 403 (Forbidden), You have already retweeted this tweet. 
Twitter API returned a 403 (Forbidden), You have already retweeted this tweet. 
Twitter API returned a 403 (Forbidden), You have already retweeted this tweet. 
Twitter API returned a 403 (Forbidden), You have already retweeted this tweet. 
Twitter API returned a 403 (Forbidden), You have already retweeted this tweet. 
Twitter API returned a 403 (Forbidden), You have already retweeted this tweet. 
Twitter API returned a 403 (Forbidden), You have already retweeted this tweet. 
Twitter API returned a 403 (Forbidden), You have already retweeted this tweet. 

所以tweet['retweeted']做回false即使辛苦也應該已經true
我在做什麼錯?

回答

0

如果您要檢查您看到的推文是原創還是轉推,則使用密鑰'retweeted'。例如,如果我發佈帖子,並且查看我的帖子'retweeted'將會是False,但是如果我從其他人轉推,'retweeted'的值將是True

我對防止多次轉發的想法是在文本文件中編寫推文id,並在每次轉發文件時檢查是否已經轉發。我知道這根本沒有效率,但我不確定任何其他解決方案。爲了讓它更快一點,您可以每天創建一個新文本,以最大限度地減少您運行檢查的ID數量。我希望我幫你