簡體   English   中英

使用tweepy從已識別的推文中獲取經/緯度坐標; 獲取KeyError:“坐標”

[英]Getting lat/long coordinates from identified tweets using tweepy; getting KeyError: 'coordinates'

我正在嘗試從已識別的推文中獲取經度/緯度坐標。 我遇到麻煩的部分是if decoded['coordinates']!=None: t.write(str(decoded['coordinates']['coordinates'])塊。我不知道它是否有效或並不是因為有時在返回錯誤之前, ~150條tweet會以[None]坐標返回,所以我相信當找到一條帶有坐標的tweet時,錯誤會再次出現,然后返回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'])

有什么幫助嗎? 謝謝。

並非所有tweet對象都包含“ coordinates”鍵,因此您必須使用以下內容檢查它是否存在:

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

另外,請注意:

“與單例的比較(如None)應始終使用'is'或'is not'進行,永遠不要使用相等運算符。”

PEP 8

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM