代码之家  ›  专栏  ›  技术社区  ›  D. Wei

如何获取包含多个关键字的推文数据

  •  1
  • D. Wei  · 技术社区  · 8 年前

    我试图通过使用这些典型代码来积累推特数据。正如你所见,我试图跟踪包含“UniversalStudio”、“迪斯尼乐园”或“洛杉矶”的推特。但事实上,我真正想得到的是包含这些关键词“UniversalStudios”、“Disneyland”和“LosAngeles”的推特。有人能告诉我如何做到这一点吗?

    提前多谢:)

    #This is a basic listener that just prints received tweets to stdout.
    class StdOutListener(StreamListener):
    
        def on_data(self, data):
            all_data = json.loads(data)
            tweet = TextBlob(all_data["text"])
    
            #Add the 'sentiment data to all_data
            #all_data['sentiment'] = tweet.sentiment
    
            #print(tweet)
            #print(tweet.sentiment)
    
            # Open json text file to save the tweets
            with open('tweets.json', 'a') as tf:
                # Write a new line
                tf.write('\n')
    
                # Write the json data directly to the file
                json.dump(all_data, tf)
                # Alternatively: tf.write(json.dumps(all_data))
            return True
    
        def on_error(self, status):
            print (status)
    
    
    if __name__ == '__main__':
    
        #This handles Twitter authetification and the connection to Twitter Streaming API
        l = StdOutListener()
        auth = OAuthHandler(consumer_key, consumer_secret)
        auth.set_access_token(access_token, access_token_secret)
        stream = Stream(auth, l)
    
        #This line filter Twitter Streams to capture data by the keywords: 'python', 'javascript', 'ruby'
        stream.filter(languages = ['en'], track=['UniversalStudios','Disneyland', "LosAngeles"])
    
    1 回复  |  直到 8 年前
        1
  •  5
  •   Reut Sharabani    8 年前

    Twitter's API (参见“跟踪”)您需要 空间 短语之间表示AND(逗号为ORs)。我不确定您使用的库如何处理它,但我敢打赌:

    track=['UniversalStudios Disneyland LosAngeles']
    

    文档中的引用:

    通过此模型,您可以将逗号视为逻辑OR,而空格等效于逻辑AND(例如,twitter是AND twitter,twitter是the OR twitter)。