1

我对 tweepy api 有一些问题。

我只是想写一个小应用程序,它可以让我获得一个用户(或更多)的状态流,但是从一个开始就可以了 ;-)

现在:我的代码是这样的:

    def main():
       config = ConfigParser.ConfigParser()
       config.read('twitter.cfg')

       username = config.get('Twitter', 'username')
       password = config.get('Twitter', 'password') 
           listener = StreamWatcherListener()

       stream = tweepy.Stream(username, password, listener, timeout=None)
       stream.filter('132897940')

在 StreamWatcherListener 中,我有一个方法“on_status”,它打印状态文本,每当有新状态到达时(当我尝试使用 stream.sample() 而不是 stream.filter() 时,一切似乎都正常)

给定的 ID 是我的测试帐户,所以每当我发推文时,我都应该在控制台中得到一些响应……但什么也没有发生。

当我尝试

curl -d @following http://stream.twitter.com/1/statuses/filter.json -uAnyTwitterUser:Password

在终端中,我可以在 twitter api 中找到,一切运行良好。

所以也许我错误地使用了 filter() 方法?

有什么建议么?

-安迪

4

2 回答 2

7

我自己发现了

stream.filter()方法需要一个数组

所以我不得不编码

stream.filter(['1234567'])

等等

于 2010-04-16T18:32:18.353 回答
0
class TweetListener(StreamListener):
    def on_status(self,status):           
        print "TWEET ARRIVED!!!"
        print "Tweet Text : %s" % status.text
        print "Author's name : %s" % status.author.screen_name
        print "Time of creation : %s" % status.created_at
        print "Source of Tweet : %s" % status.source    
        time.sleep(10)       
        return True

    def on_error(self, status):        
       print status
       if status == 420:
            print "Too soon reconnected, Exiting!!"
            return False
        sys.exit()

def search_tweets():
    twitterStream = Stream(connect().auth, TweetListener())        
    twitterStream.filter(track=['Cricket','Maths','Army','Sports'],languages = ["en"],async=True)

这里我使用了 async 参数,它在不同的线程上运行每个流。有关文档或更多详细信息,请参阅此链接

于 2017-06-22T20:48:19.900 回答