使用Tweepy按位置排序推文

4 投票
1 回答
2581 浏览
提问于 2025-04-17 19:54

使用tweepy这个工具,我们可以根据地点来搜索推文,还可以同时搜索多个地点,比如:

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

但是当我想把纽约的所有推文放到一个列表里,把旧金山的推文放到另一个列表时,我发现推文无法被正确地分配到这两个列表中。这是我的代码片段:

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 个回答

3

你可能已经找到答案了,但我也曾对此感到困惑,经过一段时间后找到了一种简单的解决办法。

下面是我如何检查坐标,并决定将推文的数据放入英国或美国数据库的方法。

这里的“coordinates”这个关键词后面跟着的 [1] 或 [2] 是用来获取推文中的经纬度信息,并将其与下面一行中设定的范围进行比较。

希望这对你有帮助 : )

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

撰写回答