【问题标题】:Getting coordinates and place names with Twython streaming使用 Twython 流获取坐标和地名
【发布时间】:2013-09-30 07:34:44
【问题描述】:

在我的研究中,我试图从与某些关键字相关的推文中获取坐标或地名。我几乎没有编程经验,在 Python 中更是如此,但我正在尝试:)。我正在使用 Twython 来传输推文。我能够得到推文、tweetid 和用户名,但不能得到地点坐标或地名。

到目前为止,我得到的是来自 Twython 的标准流媒体线路,稍作修改:

APP_KEY = '***'
APP_SECRET = '***'
OAUTH_TOKEN = '***'
OAUTH_TOKEN_SECRET = '***'

from pprint import pprint
from twython import TwythonStreamer

class MyStreamer(TwythonStreamer):
    def on_success(self, data):
        if 'text' in data:
            print data['text'].encode('utf-8')

        if 'id_str' in data:
            print 'Tweetid: '+ data['id_str'].encode('utf-8')

        if 'user' in data:
            print 'Username:  '+ data['user']['screen_name'].encode('utf-8')

        print data['coordinates']['coordinates']

        print    
        print '-----------------------------------------------'
        print

    def on_error(self, status_code, data):
        print status_code
        self.disconnect()

stream = MyStreamer(APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET)

stream.statuses.filter(track='ajax')

我得到的错误是: "打印数据['坐标']['坐标'] TypeError: 'NoneType' 对象没有属性 'getitem'"

现在我可以看到您可能无法使用相同的方法来调用坐标?但这就是我的编程知识结束的地方,可悲的是:)。解决方案可能很简单,但在任何地方都找不到!

非常感谢!

亲切的问候,

吉姆

【问题讨论】:

    标签: python twitter streaming coordinates twython


    【解决方案1】:

    https://dev.twitter.com/docs/platform-objects/tweets

    坐标可以为空,这意味着它们不一定存在。 您可以像测试其他字段一样测试它们的存在:

    if 'coordinates' in data:
        ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-06
      相关资源
      最近更新 更多