Python - 你如何遍历字典来检查特定文本的值?

Python - How do you loop through a dictionary to check the values for certain text?

我有以下词典:

{'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 循环或列表理解,它将搜索字典中的每个值并查找特定文本(例如,“Strict-Transport-Security”)。循环后,我希望它打印密钥,然后打印搜索结果。因此,该字典的示例期望输出为:

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")