【发布时间】:2020-03-24 11:50:37
【问题描述】:
我有一个数组存储头信息:
{'x-frame-options': {'defined': True, 'warn': 0, 'contents': 'SAMEORIGIN'}, 'strict-transport-security': {'defined': True, 'warn': 0, 'contents': 'max-age=15552000'}, 'access-control-allow-origin': {'defined': False, 'warn': 1, 'contents': ''}, 'content-security-policy': {'defined': True, 'warn': 0, 'contents': "upgrade-insecure-requests; frame-ancestors 'self' https://stackexchange.com"}, 'x-xss-protection': {'defined': False, 'warn': 1, 'contents': ''}, 'x-content-type-options': {'defined': False, 'warn': 1, 'contents': ''}}
我想获取字典的第一个元素
#header is a return array that store all header information,
headers = headersecurity.verify_header_existance(url, 0)
for header in headers:
if header.find("x-frame-options"):
for headerSett in header:
defined = [elem[0] for elem in headerSett.values()] # here I don't get first element
print(defined)
预期结果是:
x-frame-options : defined = True;
access-control-allow-origin : defined = True;
x-content-type-options : defined = True;
....
谢谢
【问题讨论】:
-
标题中根本没有数组,所以没有first元素的办法。请说明您期望的价值
-
dicts 是无序的。没有“第一”。为什么不能使用它的键索引它? -
我要获取值:已定义
-
...所以使用
"defined"作为您的密钥。elem["defined"],不是elem[0]。 -
但我希望它以我为所有其他标题定义的方式
标签: python http-headers