【问题标题】:Twitter feed Continuous data collection with intermittent updatesTwitter 提要 间歇性更新的持续数据收集
【发布时间】:2016-01-21 18:24:54
【问题描述】:

我正在收集推特数据。我按指定的术语“生日”过滤流。我希望能够在数据收集的同时计算频率,而不会中断它。 现在它只是打印所有内容。

如果我要创建另一个函数 def data_processing() 来计算词频,我如何从 def on_data(self, data) 访问 z= nltk.word_tokenize(extracted) ?

    import tweepy
    import json
    import nltk
    import time

    # counting
    import numpy
    from collections import Counter



    # Authentication details. To  obtain these visit dev.twitter.com

    consumer_key = XXX
    consumer_secret = XXX
    access_token = XXX
    access_token_secret = XXX
    sequence=[]

    # This is the listener, resposible for receiving data
    class StdOutListener(tweepy.StreamListener):



    def on_data(self, data):
     # Twitter returns data in JSON format - we need to decode it first
    decoded = json.loads(data)


    # Also, we convert UTF-8 to ASCII ignoring all bad characters sent         by users
   decoded['text'].encode('latin1', 'ignore')))

            extracted=decoded['text'].encode('ascii', 'ignore').decode('ascii')
            z= nltk.word_tokenize(extracted)


                    print(z)
            return True


    if __name__ == '__main__':

        l = StdOutListener()
        auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
        auth.set_access_token(access_token, access_token_secret)

        print ("Showing all new tweets for #programming:")

        # There are different kinds of streams: public stream, user stream, multi-user streams
        # In this example follow #programming tag
        # For more details refer to https://dev.twitter.com/docs/streaming-apis
        stream = tweepy.Stream(auth, l, timeout=60)
        stream.filter(track=['birthday'])

【问题讨论】:

    标签: python twitter nltk tweepy


    【解决方案1】:

    print 中的 on_data 函数。

    所以它当然会在每次收到数据时打印。

    相反,删除 print,等到 5 分钟结束后,停止流,并在流停止后打印。

    【讨论】:

    • 对不起,我读了你的问题,好像你想停下来,只处理 5 分钟的样本。然后不要停止,直接打印。
    【解决方案2】:

    使用

    import time
    time.sleep(5*60) 
    

    暂停执行5分钟

    【讨论】:

      猜你喜欢
      • 2010-11-29
      • 2012-04-07
      • 2016-07-28
      • 1970-01-01
      • 1970-01-01
      • 2014-10-09
      • 1970-01-01
      • 1970-01-01
      • 2017-03-10
      相关资源
      最近更新 更多