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

时间:2018-02-28 10:28:55

标签: python python-3.x twitter stream tweepy

我试图通过使用这些典型代码来累积推文数据。正如您所看到的,我试图跟踪包含' UniversalStudios'迪士尼乐园'或者洛杉矶'。但事实上,我真正希望得到的是包含这些关键词的推文" UniversalStudios"," Disneyland" AND" 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 个答案:

答案 0 :(得分:3)

Twitter's API(请参阅" track")提到您需要在短语之间使用空格来表示AND(逗号是OR)。我不确定您使用的库如何处理它,但我的赌注是:

<af:table value="#{bindings.RgcResultsMonitor1.collectionModel}" var="row"
                  rows="#{bindings.RgcResultsMonitor1.rangeSize}"
                  emptyText="#{bindings.RgcResultsMonitor1.viewable ? 'No data to display.' : 'Access Denied.'}"
                  rowBandingInterval="0"
                  selectedRowKeys="#{bindings.RgcResultsMonitor1.collectionModel.selectedRow}"
                  selectionListener="#{bindings.RgcResultsMonitor1.collectionModel.makeCurrent}" fetchSize="#{bindings.RgcResultsMonitor1.rangeSize}" id="t2"
                  contentDelivery="immediate"
                  rowSelection="single">

文档引用:

  

通过这个模型,你可以将逗号视为逻辑OR,而空格等同于逻辑AND(例如'twitter'是AND twitter,',twitter'是OR twitter)。