我想从某个站点检索所有标题,在这个示例中“https://www.facebook.com”如下所示:

import urllib2
enter code here`req = urllib2.Request('https://www.facebook.com/')
res = urllib2.urlopen(req)
print res.info()
res.close();

结果是:
X-XSS-Protection: 0
Pragma: no-cache
Cache-Control: private, no-cache, no-store, must-revalidate
X-Frame-Options: DENY
Strict-Transport-Security: max-age=15552000; preload
X-Content-Type-Options: nosniff
Expires: Sat, 01 Jan 2000 00:00:00 GMT
Set-Cookie: sb=1GyeWkJzGbmX-VUyBi26; expires=Thu, 05-Mar-2020 10:26:28 GMT; Max-Age=63071999; path=/; domain=.facebook.com; secure; httponly
Vary: Accept-Encoding
Content-Type: text/html; charset=UTF-8
X-FB-Debug: X9aSOOKs6/aER1yuY4iUUIZrj4yTKtKSUAZ/AFE37IieCe8O4MSsFc5xlQ0LoQyHnbrSL4DaYiTVUUkFZeDrsqqg==
Date: Tue, 06 Mar 2018 10:26:29 GMT
Connection: close

我可以检索除内容安全策略(CSP)以外的所有标题;
但每当我测试geekflare csp test
它成功地检索了所有头,包括csp头。

最佳答案

似乎我忘记在请求中设置用户代理。

关于python - 无法使用python检索CSP header ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49128731/

10-12 20:14