twitter流JSON解码

时间:2015-05-15 07:35:13

标签: python json twitter tweepy

所以,我创建了地理地图框,我从中收集了推文,我希望使用long; lat来获得更精确的位置。

我需要只拿出坐标(长;拉特分开)而没有其余的"坐标"数据

我正在使用 tweepy ,我理解我没有正确解码,但我似乎无法理解为什么它不起作用。

这就是我在哪里以及如何继续失败

输入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
      ]
   },

我的代码

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])

错误

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__'

1 个答案:

答案 0 :(得分:0)

通过查看其他示例,看起来您在on_data中收到的参数已经被解析为dict,而不是原始JSON。因此,没有JSON可供阅读,因此tweet最终为空。

快速简单的解决方法是更改​​

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

简单地

def on_data(self, tweet):

从那里拿走。

我还注意到你的边界框的坐标似乎是错误的顺序 - 位置应该由西南和东北坐标指定。