使用 urllib.request 和 json 模块在 Python 中加载 JSON 对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32795460/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Loading JSON object in Python using urllib.request and json modules
提问by David R?desj?
I am having problems making the modules 'json' and 'urllib.request' work together in a simple Python script test. Using Python 3.5 and here is the code:
我在一个简单的 Python 脚本测试中使模块 'json' 和 'urllib.request' 一起工作时遇到问题。使用 Python 3.5,这里是代码:
import json
import urllib.request
urlData = "http://api.openweathermap.org/data/2.5/weather?q=Boras,SE"
webURL = urllib.request.urlopen(urlData)
print(webURL.read())
JSON_object = json.loads(webURL.read()) #this is the line that doesn't work
When running script through command line the error I am getting is "TypeError:the JSON object must be str, not 'bytes'". I am new to Python so there is most likely a very easy solution to is. Appreciate any help here.
当通过命令行运行脚本时,我得到的错误是“ TypeError:the JSON object must be str, not 'bytes'”。我是 Python 的新手,所以很可能有一个非常简单的解决方案。感谢这里的任何帮助。
采纳答案by Martijn Pieters
Apart from forgetting to decode, you can only read the response once. Having called .read()
already, the second call returns an empty string.
除了忘记解码之外,您只能读取一次响应。已经调用过.read()
,第二次调用返回一个空字符串。
Call .read()
just once, and decodethe data to a string:
.read()
只调用一次,并将数据解码为字符串:
data = webURL.read()
print(data)
encoding = webURL.info().get_content_charset('utf-8')
JSON_object = json.loads(data.decode(encoding))
The response.info().get_content_charset()
calltells you what characterset the server thinks is used.
该response.info().get_content_charset()
调用会告诉您服务器认为使用了什么字符集。
Demo:
演示:
>>> import json
>>> import urllib.request
>>> urlData = "http://api.openweathermap.org/data/2.5/weather?q=Boras,SE"
>>> webURL = urllib.request.urlopen(urlData)
>>> data = webURL.read()
>>> encoding = webURL.info().get_content_charset('utf-8')
>>> json.loads(data.decode(encoding))
{'coord': {'lat': 57.72, 'lon': 12.94}, 'visibility': 10000, 'name': 'Boras', 'main': {'pressure': 1021, 'humidity': 71, 'temp_min': 285.15, 'temp': 286.39, 'temp_max': 288.15}, 'id': 2720501, 'weather': [{'id': 802, 'description': 'scattered clouds', 'icon': '03d', 'main': 'Clouds'}], 'wind': {'speed': 5.1, 'deg': 260}, 'sys': {'type': 1, 'country': 'SE', 'sunrise': 1443243685, 'id': 5384, 'message': 0.0132, 'sunset': 1443286590}, 'dt': 1443257400, 'cod': 200, 'base': 'stations', 'clouds': {'all': 40}}
回答by Jay Patel
As I study myself you just need to use decode('utf-8')
function, then after use json.load()
function to extract into json format.
在我学习的过程中,你只需要使用decode('utf-8')
函数,然后使用json.load()
函数提取成json格式。
>>> import json
>>> import urllib.request
>>> urlData = "http://api.openweathermap.org/data/2.5/weather?q=Boras,SE"
>>> webURL = urllib.request.urlopen(urlData)
>>> data = webURL.read()
>>> JSON_object = json.loads(data.decode('utf-8'))
{'coord': {'lat': 57.72, 'lon': 12.94}, 'visibility': 10000, 'name': 'Boras', 'main': {'pressure': 1021, 'humidity': 71, 'temp_min': 285.15, 'temp': 286.39, 'temp_max': 288.15}, 'id': 2720501, 'weather': [{'id': 802, 'description': 'scattered clouds', 'icon': '03d', 'main': 'Clouds'}], 'wind': {'speed': 5.1, 'deg': 260}, 'sys': {'type': 1, 'country': 'SE', 'sunrise': 1443243685, 'id': 5384, 'message': 0.0132, 'sunset': 1443286590}, 'dt': 1443257400, 'cod': 200, 'base': 'stations', 'clouds': {'all': 40}}