如何阻止推文重复?

时间:2018-02-01 05:25:58

标签: python-3.x twitter tweepy

我正在运行一个代码,该代码从Twitter API获取推文并将其保存到txt文件中。代码如下:

from tweepy import Stream
from tweepy import OAuthHandler
from tweepy.streaming import StreamListener
import json
import sentmod as s

 #consumer key, consumer secret, access token, access secret.
ckey= "xxxxxxxxxxxxxxxxxxx"
csecret="xxxxxxxxxxxxxxxxxxx"
atoken="xxxxxxxxxxxxxxxxxxx"
asecret="xxxxxxxxxxxxxxxxxxx"

class listener(StreamListener):
   def on_data(self, data):
      all_data = json.loads(data)
      tweet = all_data["text"]
      sentiment_value, confidence = s.sentiment(tweet)
      tweet.encode('ascii', 'ignore')
      tweets= open("tweets.txt","a",encoding="utf-8")
      tweets.write(tweet)
      tweets.write('\n\n\n')
      tweets.close()
     print(tweet, sentiment_value, confidence)
     if confidence*100 >= 60:
        output = open("twitter-out.txt","a")
        output.write(sentiment_value)
        output.write('\n\n\n')
        output.close()
        return True

def on_error(self, status):
    print(status)

auth = OAuthHandler(ckey, csecret)
auth.set_access_token(atoken, asecret)

twitterStream = Stream(auth, listener())
twitterStream.filter(track=["Car"],languages=['en'])  #locations=[]

当我将推文添加到文本文件时,推文有时会重复推文,我该如何解决?

0 个答案:

没有答案