写了一个python脚本来收集网站列表的响应头。现在,我有json文件,其中包含response header
网站。 1
是ID (key)
,其余所有values
都是针对特定网址解析的响应标头。
{
"1": {
"transfer-encoding": "chunked",
"set-cookie": "lng=hi; path=/; expires=Tue, 14-Feb-2017 08:54:15 GMT; domain=.ucoz.net;, uSID=HYv2D8GG3nmptnLPRZbxuxBHi5awMLA8pwrSRf2aE8t0l9%3BEeTKJkgoo; path=/; domain=www.ucoz.net;",
"keep-alive": "timeout=15",
"server": "nginx/1.8.0",
"connection": "keep-alive",
"date": "Mon, 15 Feb 2016 08:54:15 GMT",
"content-type": "text/html; charset=UTF-8"
}
}
如何获取密钥1
结果:
1,nginx/1.8.0
答案 0 :(得分:3)
使用JSON encoder解析文件中的数据:
import json
from pprint import pprint
with open('json_data.json') as json_data:
data = json.load(json_data)
pprint(data)
# now you can traverse your data dict....
print(data['1']['server'])
答案 1 :(得分:3)
json.loads
:将data
(包含JSON的实例)反序列化为Python对象。
>>> import json
>>> data = ''' {
... "1": {
... "transfer-encoding": "chunked",
... "set-cookie": "lng=hi; path=/; expires=Tue, 14-Feb-2017 08:54:15 GMT; domain=.ucoz.net;, uSID=HYv2D8GG3nmptnLPRZbxuxBHi5awMLA8pwrSRf2aE8t0l9%3BEeTKJkgoo; path=/; domain=www.ucoz.net;",
... "keep-alive": "timeout=15",
... "server": "nginx/1.8.0",
... "connection": "keep-alive",
... "date": "Mon, 15 Feb 2016 08:54:15 GMT",
... "content-type": "text/html; charset=UTF-8"
... }
... }'''
>>> d = json.loads(data)
>>> d['1']['server']
u'nginx/1.8.0'