使用Tweepy按位置对推文进行排序

时间:2013-03-21 17:01:42

标签: python python-2.7 twitter tweepy

使用tweepy,我们可以按位置和多个位置搜索推文,例如:

stream.filter(locations=[-122.75,36.8,-121.75,37.8,-74,40,-73,41], track=terms)

然而,当我尝试将NY中的所有推文放在一个列表中,而将SF中的推文放在另一个列表中时,我不能将它们分配到任何一个列表中。这是我的代码片段:

NY = 74,40,-73,41
NewYorkCNN = []
if status.coordinates == NY or status.place == 'New York':
    for term in terms:
        if term in status.text:
            NewYorkCNN.append(term)

如何将它们正确放入正确的清单?

1 个答案:

答案 0 :(得分:3)

你现在可能已经找到了答案,但这也是我一直在努力的事情,经过一段时间后发现了一个简单的解决方案。

以下是检查坐标的方法,并确定是否将推文数据分配到英国或美国数据库。

后跟[1]或[2]的“coordinates”关键字从推文中获取纬度/经度元数据,并将其与下面一行中指定的边界进行比较。

我希望这会有所帮助:)

class CustomStreamListener(tweepy.StreamListener):

  def check_ukcoords(self, status):
      if status.coordinates is not None:
          lat = status.coordinates['coordinates'][1]
          lng = status.coordinates['coordinates'][0]        
          return 49.7 < lat < 58.8 and -6.1 < lng < 1.7

  def check_uscoords(self, status):
      if status.coordinates is not None:
          lat = status.coordinates['coordinates'][1]
          lng = status.coordinates['coordinates'][0]        
          return 25.1 < lat < 49.1 and -125 < lng < -60.5
相关问题