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

标签 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 = 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] 或 [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

关于python - 使用 Tweepy 按位置对推文进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15553667/

相关文章:

python-2.7 - 安装pocketsphinx Python时找不到Swig

python - Pandas 从 dataframe.loc 获取索引列表

oauth - "Could not authenticate you."- 使用 Twitter OAuth 时出错

python - 将 float 保存到文件中

python - Pandas groupby + ifelse + 将新列添加回原始 df

Python/CSV 唯一行,列中每行具有唯一值

python - 删除主机名的公共(public)元素(缩短主机名)- DRY

python - 混洗数据框中的列

ios - SLComposeViewController (twitter) 和 TWTweetComposeViewController 的字符计数不准确

iphone - Twitter+oAuth 引擎在尝试发布状态时返回 401