TypeError 从 urllib ipython 解析 JSON obj

TypeError parsing JSON obj from urllib ipython

我正在使用 API 从网站请求数据。可以查到数据here and pasted into a JSON Viewer。我的代码及其返回的错误如下。我猜这是一个快速修复,部分反映了这是我第一次使用 urllib 的事实。

import pandas as pd
import urllib 
import json

api_key = '79bf8eb2ded72751cc7cda5fc625a7a7'
url = 'http://maplight.org/services_open_api/map.bill_list_v1.json?apikey=79bf8eb2ded72751cc7cda5fc625a7a7&jurisdiction=us&session=110&include_organizations=1&has_organizations=1'

json_obj = urllib.request.urlopen(url)

data = json.load(json_obj)

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-21-85ab9af07320> in <module>()
      8 json_obj = urllib.request.urlopen(url)
      9 
---> 10 data = json.load(json_obj)

/home/jayaramdas/anaconda3/lib/python3.5/json/__init__.py in load(fp, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
    266         cls=cls, object_hook=object_hook,
    267         parse_float=parse_float, parse_int=parse_int,
--> 268         parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
    269 
    270 

/home/jayaramdas/anaconda3/lib/python3.5/json/__init__.py in loads(s, encoding, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
    310     if not isinstance(s, str):
    311         raise TypeError('the JSON object must be str, not {!r}'.format(
--> 312                             s.__class__.__name__))
    313     if s.startswith(u'\ufeff'):
    314         raise JSONDecodeError("Unexpected UTF-8 BOM (decode using utf-8-sig)",

TypeError: the JSON object must be str, not 'bytes'

如有任何建议、意见或进一步的问题,我们将不胜感激。

json.load 不会猜测编码,因此您通常需要 .read 返回对象中的字节,然后使用 .decode 和适当的编解码器。例如:

data = json.loads(json_obj.read().decode('utf-8'))

official documentation 中有一个这样的例子。

具体地说:

Note that urlopen returns a bytes object. This is because there is no way for urlopen to automatically determine the encoding of the byte stream it receives from the http server. In general, a program will decode the returned bytes object to string once it determines or guesses the appropriate encoding.