【问题标题】:Coordinates from twitter location data来自推特位置数据的坐标
【发布时间】:2018-01-16 16:55:07
【问题描述】:

我正在使用 Tweepy 提取推文的坐标。使用json加载后是这样的。

tweet = json.loads(data)
print(tweet['coordinates'])
{'coordinates': [-86.771035, 39.514024], 'type': 'Point'}

我想将经度和纬度存储到数据库中,如何直接访问坐标?我尝试使用 tweet['coordinates'][0] 和各种变体,但它似乎没有按我想要的方式工作。

【问题讨论】:

  • tweet['coordinates'] 是否返回其他字典?
  • 所以,试试 tweet['coordinates']['coordinates']。这很奇怪,但如果它返回像您的问题一样的其他字典,它可能会起作用。
  • lat = tweet['coordinates']['cordinates'][0] 和 lng = tweet['coordinates']['cordinates'][1] 应该可以工作

标签: python python-3.x twitter tweepy


【解决方案1】:

您可以通过以下方式获取坐标数据:

for tweet in tweepy.Cursor(api.search,q='love',count = num_tweets).items():
    if (tweet.coordinates is not None): 
        lon = tweet.coordinates['coordinates'][0]
        lat = tweet.coordinates['coordinates'][1]

这很奇怪,但我必须尝试多种形式和方法才能使其发挥作用。

【讨论】:

    【解决方案2】:

    试试:

    latitude, longitude = tweet["coordinates"]["coordinates"]
    

    因为 tweet["coordinates"] 返回其他字典,其键也称为 "coordinates",其值为列表。

    【讨论】:

    • 不适合我,我得到 TypeError: 'Status' object has no attribute 'getitem'
    猜你喜欢
    • 2017-09-27
    • 2021-05-15
    • 2011-12-12
    • 2016-09-02
    • 1970-01-01
    • 2011-10-29
    • 2014-07-16
    • 2013-09-09
    • 2021-02-08
    相关资源
    最近更新 更多