Twitter Streaming API:是否可以仅过滤具有坐标的推文?

时间:2012-08-21 09:06:41

标签: python api twitter streaming

我有以下代码可以过滤特定的位置范围,但我真的希望它只返回具有位置坐标的推文。如果没有,它会返回'None',但我想完全忽略它们。有什么想法吗?

  loc = [-3.72,50.29,-3.42,50.41]

class CustomStreamListener(tweepy.StreamListener):
    def on_status(self, status):

        try:
            print "%s\t%s\t%s\t%s\t%s" % (status.text,
                                      status.author.screen_name,
                                      status.created_at,
                                      status.source,
                                      status.coordinates)          

        except Exception, e:
            print >> sys.stderr, 'Encountered Exception:', e
            pass

    def on_error(self, status_code):
        print >> sys.stderr, 'Encountered error with status code:', status_code
        return True # Don't kill the stream

    def on_timeout(self):
        print >> sys.stderr, 'Timeout...'
        return True # Don't kill the stream

streaming_api = tweepy.streaming.Stream(auth, CustomStreamListener(), timeout=60)

streaming_api.filter(follow=None, locations=loc)

1 个答案:

答案 0 :(得分:0)

如果你只需要从列表中删除None元素,你可以这样做:

>>> l = ['a', 'b', None, 'c', None]
>>> filter(None, l)
['a', 'b', 'c']