Closed. This question needs to be more focused。它当前不接受答案。
                            
                        
                    
                
                            
                                
                
                        
                            
                        
                    
                        
                            想改善这个问题吗?更新问题,使其仅通过editing this post专注于一个问题。
                        
                        4年前关闭。
                                                                                            
                
        
我正在尝试编写一个模块,该模块从open exchange rates获取货币列表并将其作为字典返回。我尝试使用urllib模块,但我的知识有限。

到目前为止,这就是我所拥有的:

import urllib.request
response = urllib.request.urlopen('http://openexchangerates.org/api/currencies.json')
html = response.read()
print(html)

最佳答案

html变量中包含的是JSON字符串。只需将其解析为字典即可:

import json
import urllib.request
response = urllib.request.urlopen('http://openexchangerates.org/api/currencies.json')
html = response.read().decode("utf-8")

my_dict = json.loads(html)
print my_dict

10-06 15:47