我正在使用API從網站請求數據。數據可以在here找到並粘貼到JSON Viewer。下面是我的代碼以及它返回的錯誤。我猜測這是一個快速修復,部分反映了這是我第一次使用urllib。TypeError從urllib解析JSON obj ipython
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'
任何建議,意見,或其他問題的讚賞。
它看起來要'str',不'bytes'。你有沒有嘗試'json.load(json_obj.decode('utf-8'))'? – mgilson
謝謝您的建議!我剛剛嘗試了你的建議。我收到以下錯誤'AttributeError:'HTTPResponse'對象沒有屬性'編碼'' –
糟糕...''json.loads(json_obj.read()。decode('utf-8'))'也許? – mgilson