gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 06:35:20 24 4
gpt4 key购买 nike

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

  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']

关于python - 推特流媒体 API : Is it possible to filter only tweets that have co-ordinates?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12051574/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com