【问题标题】:twitter stream JSON decodingtwitter 流 JSON 解码
【发布时间】:2015-07-27 01:19:11
【问题描述】:

因此,我创建了用于收集推文的地理地图框,并且我想使用 long;lat 获得更精确的位置。

我只需要获取坐标(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__'

【问题讨论】:

  • 你能添加loc3.py的代码吗?
  • @wanderlust 嗯,这不是“我的代码”部分的内容吗?
  • @wanderlust 是的,基本上我只省略了导入和授权

标签: python json twitter tweepy


【解决方案1】:

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

快速简单的解决方法是改变

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

进入简单

def on_data(self, tweet):

然后从那里拿走。

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

【讨论】:

  • 感谢您的洞察力,但是当我这样做时,它给了我错误string indices must be integers 这个text = tweet[0] 也不行。我在 python 中尝试了子格式,就像在文档中一样 - print tweet["geo"]["coordinates"][0] 在测试子字符串上运行良好,但在流中没有。
  • 此代码无法生成该错误消息。该对象不是字符串(除非您进行了不相关的更改)并且索引 一个整数。
  • 我唯一改变的是 def on_data(self, data): tweet = json.loads(data)def on_data(self, tweet): 并且它给出了关于 text = tweet['text'] 的错误 tring indices must be integers 我没有改变任何代码就像上面这些简单的改变一样
猜你喜欢
  • 1970-01-01
  • 2015-10-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-09
  • 2013-12-07
  • 2013-01-19
相关资源
最近更新 更多