【问题标题】:Using Tweepy to sort tweets by location使用 Tweepy 按位置对推文进行排序
【发布时间】:2013-03-11 07:48:30
【问题描述】:

使用 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)

如何正确地将它们放在正确的列表中?

【问题讨论】:

  • 如果问题仍未解决,能否提供您正在使用的完整代码?

标签: python python-2.7 twitter tweepy


【解决方案1】:

您现在可能已经找到了答案,但这也是我一直在努力解决的问题,过了一会儿找到了一个足够简单的解决方案。

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

后跟 [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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-10
    • 2014-06-09
    相关资源
    最近更新 更多