如何使用 urllib3 直接从 Python 中的 github 读取此 json 文件?
How to read this json file directly from github in Python with urllib3?
我正在尝试使用
从 github 导入特定的 json 文件
import urllib3
import json
http = urllib3.PoolManager()
url = 'https://raw.githubusercontent.com/leanhdung1994/BigData/main/fr-esr-principaux-etablissements-enseignement-superieur.json'
f = http.request('GET', url)
data = json.load(f.data)
能否请您解释一下为什么会出现以下错误以及如何正确读取此文件?
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-14-cc9b55b171d7> in <module>
4 url = 'https://raw.githubusercontent.com/leanhdung1994/BigData/main/fr-esr-principaux-etablissements-enseignement-superieur.json'
5 f = http.request('GET', url)
----> 6 data = json.load(f.data)
~\anaconda3\lib\json\__init__.py in load(fp, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
291 kwarg; otherwise ``JSONDecoder`` is used.
292 """
--> 293 return loads(fp.read(),
294 cls=cls, object_hook=object_hook,
295 parse_float=parse_float, parse_int=parse_int,
AttributeError: 'bytes' object has no attribute 'read'
我会推荐更现代的 requests
库,而不是使用 urllib3
。
您的代码可能如下所示:
import requests
url = 'https://raw.githubusercontent.com/leanhdung1994/BigData/main/fr-esr-principaux-etablissements-enseignement-superieur.json'
f = requests.get(url)
# The .json() method automatically parses the response into JSON.
data = f.json()
如果要使用urllib3,必须对数据进行解码,并且要使用jsonloads(不是load)方法。例如:
import urllib3
import json
http = urllib3.PoolManager()
url = 'https://raw.githubusercontent.com/leanhdung1994/BigData/main/fr-esr-principaux-etablissements-enseignement-superieur.json'
f = http.request('GET', url)
data = json.loads(f.data.decode("utf8"))
我正在尝试使用
从 github 导入特定的 json 文件import urllib3
import json
http = urllib3.PoolManager()
url = 'https://raw.githubusercontent.com/leanhdung1994/BigData/main/fr-esr-principaux-etablissements-enseignement-superieur.json'
f = http.request('GET', url)
data = json.load(f.data)
能否请您解释一下为什么会出现以下错误以及如何正确读取此文件?
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-14-cc9b55b171d7> in <module>
4 url = 'https://raw.githubusercontent.com/leanhdung1994/BigData/main/fr-esr-principaux-etablissements-enseignement-superieur.json'
5 f = http.request('GET', url)
----> 6 data = json.load(f.data)
~\anaconda3\lib\json\__init__.py in load(fp, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
291 kwarg; otherwise ``JSONDecoder`` is used.
292 """
--> 293 return loads(fp.read(),
294 cls=cls, object_hook=object_hook,
295 parse_float=parse_float, parse_int=parse_int,
AttributeError: 'bytes' object has no attribute 'read'
我会推荐更现代的 requests
库,而不是使用 urllib3
。
您的代码可能如下所示:
import requests
url = 'https://raw.githubusercontent.com/leanhdung1994/BigData/main/fr-esr-principaux-etablissements-enseignement-superieur.json'
f = requests.get(url)
# The .json() method automatically parses the response into JSON.
data = f.json()
如果要使用urllib3,必须对数据进行解码,并且要使用jsonloads(不是load)方法。例如:
import urllib3
import json
http = urllib3.PoolManager()
url = 'https://raw.githubusercontent.com/leanhdung1994/BigData/main/fr-esr-principaux-etablissements-enseignement-superieur.json'
f = http.request('GET', url)
data = json.loads(f.data.decode("utf8"))