2013-05-14 3 views
3

JSON 파일 (fromt, stackexchange 및 stackoverflow api)에 대한 특정 URL에 액세스 중입니다. 다른 한편으로는 모든에stackexchange API를 json으로 열 때 JSON 객체를 디코딩 할 수 없습니다.

import urllib2 
import json 

url = "http://api.stackexchange.com/2.1/tags?order=desc&sort=popular&site=quant&pagesize=100&page=1"  
data = json.loads(urllib2.urlopen(url).read()) 

<ipython-input-20-7540e91a8ff2> in <module>() 
----> 1 data = json.loads(urllib2.urlopen(url).read()) 

/usr/lib/python2.7/json/__init__.pyc in loads(s, encoding, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw) 
    336    parse_int is None and parse_float is None and 
    337    parse_constant is None and object_pairs_hook is None and not kw): 
--> 338   return _default_decoder.decode(s) 
    339  if cls is None: 
    340   cls = JSONDecoder 

/usr/lib/python2.7/json/decoder.pyc in decode(self, s, _w) 
    363 
    364   """ 
--> 365   obj, end = self.raw_decode(s, idx=_w(s, 0).end()) 
    366   end = _w(s, end).end() 
    367   if end != len(s): 

/usr/lib/python2.7/json/decoder.pyc in raw_decode(self, s, idx) 
    381    obj, end = self.scan_once(s, idx) 
    382   except StopIteration: 
--> 383    raise ValueError("No JSON object could be decoded") 
    384   return obj, end 

ValueError: No JSON object could be decoded 

트위터 API를 ... 왜 함께 잘 작동 다음 json.loads() 명령을 실행하는 동안 다음과 같은 오류가 보여?

답변

4

StackExchange API always compresses its responses,하지만 파이썬 doesn't automatically uncompress it, 그래서 json은 gzipped 데이터를 얻고 있습니다.

This answer은 gzip 모듈을 사용하여 응답을 처리하는 방법을 보여줍니다.

+0

감사 톤 동생 ...을! –

+0

도움이 된 것을 기쁘게 생각합니다. 문제가 해결되면 대답을 수락하는 것을 잊지 마십시오 (왼쪽의 체크 표시를 클릭하십시오). –

6

마찬가지로 @Thomas는 the gzip compression이 원인이라고 제안합니다.
내가 당신을 위해 이런 종류의 물건에 대해 고민 requests 라이브러리를 사용하는 것이 좋습니다 :

import requests 

data_url = "https://api.stackexchange.com/2.1/search?page=1&pagesize=10&order=desc&sort=activity&tagged=pandas&site=stackoverflow" 
data_json = requests.get(data_url).json() 
관련 문제