2014-04-13 24 views
14

解析HTTP响应我想在THIS URL操作的信息。我可以成功打开它并阅读它的内容。但是我真正想要做的是抛弃所有我不想要的东西,并操纵我想要保留的东西。在Python

有没有办法将字符串转换为字典,以便我可以迭代它?或者我只需要解析它(str类型)?

from urllib.request import urlopen 

url = 'http://www.quandl.com/api/v1/datasets/FRED/GDP.json' 
response = urlopen(url) 

print(response.read()) # returns string with info 

回答

38

此问题已解决。

当我打印response.read()我注意到b被预先写入字符串(例如b'{"a":1,..)。 “b”代表字节,用作所处理对象类型的声明。既然,我知道一个字符串可以通过使用json.loads('string')转换为字典,我只需要将字节类型转换为字符串类型。我通过解码对utf-8 decode('utf-8')的响应来做到这一点。一旦它在字符串类型中,我的问题就解决了,我很容易就可以遍历dict

我不知道这是否是最快或最“pythonic”的写作方式,但它的工作原理和总是时间后优化和改进!我的解决方案的完整代码:

from urllib.request import urlopen 
import json 

# Get the dataset 
url = 'http://www.quandl.com/api/v1/datasets/FRED/GDP.json' 
response = urlopen(url) 

# Convert bytes to string type and string type to dict 
string = response.read().decode('utf-8') 
json_obj = json.loads(string) 

print(json_obj['source_name']) # prints the string with 'source_name' key 

如果有人想通过谷歌找到这个,我希望这有助于。我可以给出最好的建议,仔细阅读你的错误,并密切关注你接受的输出。

-1

我猜python 3.4中的东西已经改变了。这为我工作:

print("resp:" + json.dumps(resp.json())) 
+2

没有'json'属性。不要混淆'request'库和'urllib.request'。 – jfs

4

json作品与Unicode文本在Python 3(JSON格式本身的定义只在Unicode文本而言),因此,你需要解码的HTTP响应接收的字节。 r.headers.get_content_charset('utf-8')得到您的字符编码:

#!/usr/bin/env python3 
import io 
import json 
from urllib.request import urlopen 

with urlopen('https://httpbin.org/get') as r, \ 
    io.TextIOWrapper(r, encoding=r.headers.get_content_charset('utf-8')) as file: 
    result = json.load(file) 
print(result['headers']['User-Agent']) 

这是没有必要在这里使用io.TextIOWrapper

#!/usr/bin/env python3 
import json 
from urllib.request import urlopen 

with urlopen('https://httpbin.org/get') as r: 
    result = json.loads(r.read().decode(r.headers.get_content_charset('utf-8'))) 
print(result['headers']['User-Agent']) 
+0

在Python 3中,使用'r.msg.get_content_charset'。 https://docs.python.org/3/library/http.client.html#http.client.HTTPResponse.msg –

+0

@ PeppeL-G:来自'HTTPResponse'源文件:*“'headers'在这里使用并支持'urllib''msg'提供作为HTTP客户端向后兼容层。“* – jfs

+0

哦,对不起,我没有Python中的很多经验,但你可能是正确的。我用'HTTPResponse'类工作从'http.client'模块,我现在看到有一些差异(这个类既包含了'msg'场和'headers'场(值相同),但只有我发现了'msg'字段的文档,所以我认为'headers'是为了向后兼容而保留的。我的错误 –

6

您也可以使用Python的请求库,而不是。

import requests 

url = 'http://www.quandl.com/api/v1/datasets/FRED/GDP.json'  
response = requests.get(url)  
dict = response.json() 

现在你可以像Python字典一样操纵“字典”了。