python - No JSON object could be decoded when opening stackexchange api as json -
i accessing particular url json file (fromt stackexchange , stackoverflow api). while executing json.loads()
command shows following error:
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 none , parse_float none , 337 parse_constant none , object_pairs_hook none , not kw): --> 338 return _default_decoder.decode(s) 339 if cls 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 decoded") 384 return obj, end valueerror: no json object decoded
on other hand works fine twitter api... why?
the stackexchange api always compresses responses, python doesn't automatically uncompress it, json getting gzipped data.
this answer shows how use gzip module handle response.
Comments
Post a Comment