【问题标题】:Trying to use Twitter API but my code wont run anything尝试使用 Twitter API,但我的代码不会运行任何东西
【发布时间】:2016-08-14 14:52:39
【问题描述】:

我正在尝试使用推特 API 来传输所有推文,包括密歇根州立大学、斯巴达人和密歇根州立大学。在我弄清楚这一点后,我想使用不同的 big10 关键词。但是,我运行此代码,但无法通过 ln (*) 无论我等待多长时间都没有发生任何事情。我的代码有问题吗?或者我如何获得这些信息的显示以便我可以分析它?

谢谢!

from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
from tweepy import Stream

#Variables that contains the user credentials to access Twitter API 
access_token = "ENTER YOUR ACCESS TOKEN"
access_token_secret = "ENTER YOUR ACCESS TOKEN SECRET"
consumer_key = "ENTER YOUR API KEY"
consumer_secret = "ENTER YOUR API SECRET"


#This is a basic listener that just prints received tweets to stdout.
class StdOutListener(StreamListener):

    def on_data(self, data):
        print data
        return True

    def on_error(self, status):
        print status


if __name__ == '__main__':

#This handles Twitter authentification 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: 'MichiganState', 'Spartans', 'MSU'
    stream.filter(track=['MichiganState', 'Spartans', 'MSU'])'

【问题讨论】:

    标签: twitter streaming ipython tweepy


    【解决方案1】:

    在监听类中使用on_status(self, status)

    class StdOutListener(tweepy.StreamListener):
    
        def on_status(self, status):
            print status.text
            print status.id
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多