Skip to main content
 首页 » 编程设计

python之推特流媒体 API : Is it possible to filter only tweets that have co-ordinates

2025年05月04日9zfyouxi

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

  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) 

请您参考如下方法:

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

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