简体   繁体   English

twitter流JSON解码

[英]twitter stream JSON decoding

So, I have created geo map box from which I gather Tweets, and I want to get even more precise locations using long;lat. 因此,我创建了一个地理地图框,从中收集推文,我想使用long; lat获得更精确的位置。

I need to get out only coordinates (long;lat separate) without rest of "coordinates" data. 我只需要获取坐标(长;纬度分开),而不获取其余的“坐标”数据。

Im using tweepy and I understand Iam not decoding it right but I can't seem to understand why It doesn't work. 我正在使用tweepy ,我知道Iam无法正确解码,但我似乎无法理解为什么它不起作用。

and this where and how i keep failing 这就是我不断失败的地方和方式

input JSON 输入JSON

    {  
   u'contributors':None,
   u'truncated':False,
   u'text':   u'Stundas tikai l\u012bdz 12.00 \U0001f64c\U0001f389\U0001f389\U0001f389 (@ R\u012bgas Valsts v\u0101cu \u0123imn\u0101zija - @rvv_gimnazija in R\u012bga) https://t.co/XCp8OzqQgk',
   u'in_reply_to_status_id':None,
   u'id':599100313690320896,
   u'favorite_count':0,
   u'source':   u'<a href="http://foursquare.com" rel="nofollow">Foursquare</a>',
   u'retweeted':False,
   u'coordinates':{  
      u'type':u'Point',
      u'coordinates':[  
         24.062859,
         56.94697
      ]
   },

My Code 我的密码

class listener(StreamListener):
    def on_data(self, data):

        tweet = json.loads(data)


        #print time.time()
        text = tweet['text']
        name = tweet['user']['name']
        screenName = tweet['user']['screen_name']
        location = tweet['coordinates']['coordinates'][0]

        print name.encode('utf-8')
        print text.encode('utf-8')
        print location
        print '\n'

        # into the data file
        with open('minedData', 'a') as outfile:
            json.dump({ 'location':location, 'time': time.time(), 'screenName': screenName, 'text': text, 'name': name}, outfile, indent = 4, sort_keys=True)
            #outfile.write(',')
            outfile.write('\n')

        return True

    def on_error(self, status):
        print status


auth = OAuthHandler(ckey, csecret)
auth.set_access_token(atoken, asecret)
twitterStream = Stream(auth, listener())
twitterStream.filter(locations=[23.47,56.66,25.148411,57.407558])

The error 错误

Traceback (most recent call last):
  File "loc3.py", line 45, in <module>
    twitterStream.filter(locations=[23.47,56.66,25.148411,57.407558])
  File "/Library/Python/2.7/site-packages/tweepy/streaming.py", line 428, in filter
    self._start(async)
  File "/Library/Python/2.7/site-packages/tweepy/streaming.py", line 346, in _start
    self._run()
  File "/Library/Python/2.7/site-packages/tweepy/streaming.py", line 255, in _run
    self._read_loop(resp)
  File "/Library/Python/2.7/site-packages/tweepy/streaming.py", line 309, in _read_loop
    self._data(next_status_obj)
  File "/Library/Python/2.7/site-packages/tweepy/streaming.py", line 289, in _data
    if self.listener.on_data(data) is False:
  File "loc3.py", line 23, in on_data
    location = tweet['coordinates']['coordinates'][0]
TypeError: 'NoneType' object has no attribute '__getitem__'

By looking at other examples, it looks like the argument you receive in on_data is already parsed into a dict, not raw JSON. 通过查看其他示例,您似乎在on_data收到的参数已被解析为dict,而不是原始JSON。 So there is no JSON to read and therefore tweet ends up empty. 因此,没有要读取的JSON,因此tweet最终为空。

The quick and simple fix is to change 快速简单的解决方法是更改

def on_data(self, data):
    tweet = json.loads(data)

into simply 简单地

def on_data(self, tweet):

and take it from there. 并从那里拿走。

I also note that your coordinates for the bounding box seem to be in the wrong order -- the location should be specified by the southwest and northeast coordinates. 我还注意到边界框的坐标似乎顺序错误-位置应由西南坐标和东北坐标指定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM