Python-如何在字典中循环以检查某些文本的值



我有以下字典:

{'https://www.youtube.com/sw.js_data': ['HTTP/2 200 OK',
'Content-Type: application/json; charset=utf-8',
'X-Content-Type-Options: nosniff',
'Cache-Control: no-cache, no-store, max-age=0, must-revalidate',
'Pragma: no-cache',
'Expires: Mon, 01 Jan 1990 00:00:00 GMT',
'Date: Mon, 14 Mar 2022 17:59:34 GMT',
'Content-Disposition: attachment; filename="response.bin"; filename*=UTF-8''response.bin',
'Strict-Transport-Security: max-age=31536000',
'X-Frame-Options: SAMEORIGIN',
'Cross-Origin-Opener-Policy-Report-Only: same-origin; report-to="ATmXEA_XZXH6CdbrmjUzyTbVgxu22C8KYH7NsxKbRt94"',
'Permissions-Policy: ch-ua-arch=*, ch-ua-bitness=*, ch-ua-full-version=*, ch-ua-full-version-list=*, ch-ua-model=*, ch-ua-platform=*, ch-ua-platform-version=*',
'Accept-Ch: Sec-CH-UA-Arch, Sec-CH-UA-Bitness, Sec-CH-UA-Full-Version, Sec-CH-UA-Full-Version-List, Sec-CH-UA-Model, Sec-CH-UA-Platform, Sec-CH-UA-Platform-Version',
'Server: ESF',
'X-Xss-Protection: 0',
'Alt-Svc: h3=":443"; ma=2592000,h3-29=":443"; ma=2592000,h3-Q050=":443"; ma=2592000,h3-Q046=":443"; ma=2592000,h3-Q043=":443"; ma=2592000,quic=":443"; ma=2592000; v="46,43"'],
'https://www.google.com/client_204?&atyp=i&biw=1440&bih=849&dpr=1.5&ei=Z4IvYpTtF5LU9AP1nIOICQ': ['HTTP/2 204 No Content',
'Content-Type: text/html; charset=UTF-8',
'Strict-Transport-Security: max-age=31536000',
"Content-Security-Policy: object-src 'none';base-uri 'self';script-src 'nonce-9KQUw4dRjvKnx/zTrOblTQ==' 'strict-dynamic' 'report-sample' 'unsafe-eval' 'unsafe-inline' https: http:;report-uri https://csp.withgoogle.com/csp/gws/cdt1",
'Bfcache-Opt-In: unload',
'Date: Mon, 14 Mar 2022 17:59:10 GMT',
'Server: gws',
'Content-Length: 0',
'X-Xss-Protection: 0',
'X-Frame-Options: SAMEORIGIN',
'Set-Cookie: 1P_JAR=2022-03-14-17; expires=Wed, 13-Apr-2022 17:59:10 GMT; path=/; domain=.google.com; Secure; SameSite=none',
'Alt-Svc: h3=":443"; ma=2592000,h3-29=":443"; ma=2592000,h3-Q050=":443"; ma=2592000,h3-Q046=":443"; ma=2592000,h3-Q043=":443"; ma=2592000,quic=":443"; ma=2592000; v="46,43"']}

我很难调用并在这本字典中搜索这些键和值。我正试图编写一个for循环或列表理解,它将搜索字典中的每个值并寻找特定的文本(例如"严格的传输安全性"(。循环浏览后,我希望它打印关键字,然后打印搜索结果。因此,这个字典所需的输出示例是:

https://www.youtube.com/sw.js_data: Strict-Transport-Security missing
https://www.google.com/client_204?&atyp=i&biw=1440&bih=849&dpr=1.5&ei=Z4IvYpTtF5LU9AP1nIOICQ: Strict-Transport-Security present

希望这是有道理的。假设这是可能的,但我很难做到这一点。非常感谢。

header = 'Strict-Transport-Security'
for url in mydictionary:
if any(s.startswith(header) for s in mydictionary[url]):
print(f"{header} found for {url}")
else:
print(f"{header} missing for {url}")

我想与您分享另一种方法-您可以将字符串列表转换为字典

def parse_list(list_of_str):
res = {}
for element in list_of_str:
try:
x = element.split(":")
res[x[0]] = x[1].strip()
except IndexError:
continue
return res
parsed = {url: parse_list(l) for url, l in your_dict.items()}

现在您可以将其用作普通字典:

for url, subdict in parsed.items():
print(url, "present" if "Strict-Transport-Security" in subdict else "missing")

最新更新