使用tweepy从已识别的推文中获取纬度/经度坐标;得到KeyError:'coordinates'

时间:2015-04-29 13:14:06

标签: python twitter tweepy

我正在尝试从已识别的推文中获取纬度/经度坐标。我遇到问题的部分是if decoded['coordinates']!=None: t.write(str(decoded['coordinates']['coordinates'])块。我不确切知道它是否正常工作,因为在返回错误之前,有时~150推文将以[None]的坐标返回,所以我相信当找到带有坐标的推文时会出现错误,然后它返回KeyError: 'coordinates'

以下是我的代码:

import tweepy
import json
from HTMLParser import HTMLParser
import os

consumer_key = ''
consumer_secret = ''
access_token = ''
access_token_secret = ''

# This is the listener, resposible for receiving data
class StdOutListener(tweepy.StreamListener):
    def on_data(self, data):
        # Twitter returns data in JSON format - we need to decode it first
        decoded = json.loads(HTMLParser().unescape(data))

        os.chdir('/home/scott/810py/Project')
        t = open('hashtagHipster.txt','a')

        # Also, we convert UTF-8 to ASCII ignoring all bad characters sent by users
        #if decoded['coordinates']:

        # decoded['coordinates'] returns a few objects that are not useful,
        # like type and place which we don't want. ['coordinates'] has a
        # second thing called ['coordinates'] that returns just the lat/long.
        # it may be that the code is correct but location is so few and far
        # between that I haven't been able to capture one. This program just
        # looks for 'hipster' in the tweet. There should be a stream of tweets
        # in the shell and everytime one that has coordinates tehy should be
        # added to the file 'hashtagHipster.txt'. Let me know what you think.

        if decoded['coordinates']!=None:
            t.write(str(decoded['coordinates']['coordinates'])) #gets just [LAT][LONG]
        print '[%s] @%s: %s' % (decoded['coordinates'], decoded['user']['screen_name'], decoded['text'].encode('ascii', 'ignore'))
        print ''
        return True

    def on_error(self, status):

        print status

if __name__ == '__main__':
    l = StdOutListener()
    auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
    auth.set_access_token(access_token, access_token_secret)

    print "Showing all new tweets for #hipster:"

    # There are different kinds of streams: public stream, user stream, multi-user streams
    # In this example follow #vintage tag
    # For more details refer to https://dev.twitter.com/docs/streaming-apis
    stream = tweepy.Stream(auth, l)
    stream.filter(track=['hipster'])

有任何帮助吗?感谢。

1 个答案:

答案 0 :(得分:2)

并非所有推文对象都包含'坐标'键,所以你必须检查它是否存在这样的东西:

 if decoded.get('coordinates',None) is not None:
   coordinates = decoded.get('coordinates','').get('coordinates','')

另外,请注意:

  

"比较像单曲这样的单身人士应该始终使用''或者'不是',永远不是平等运营商。"

PEP 8