Content-type在某些请求的headers中为空
Content-type is blank in the headers of some requests
我已经 运行 这个查询之前与其他 URL 数百万(是的,数百万)次。但是,在检查以下网页的 content-type 时出现 KeyError。
代码片段:
r = requests.get("http://health.usnews.com/health-news/articles/2014/10/15/limiting-malpractice-claims-may-not-curb-costly-medical-tests", timeout=10, headers=headers)
if "text/html" in r.headers["content-type"]:
错误:
KeyError: 'content-type'
我查看了 r.headers
的内容,它是:
CaseInsensitiveDict({'date': 'Fri, 20 May 2016 06:44:19 GMT', 'content-length': '0', 'connection': 'keep-alive', 'server': 'BigIP'})
可能是什么原因造成的?
并非所有服务器都设置 Content-Type header。如果缺少默认值,请使用 .get()
检索默认值:
if "text/html" in r.headers.get("content-type", ''):
对于您提供的URL,我无法重现:
$ curl -s -D - -o /dev/null "http://health.usnews.com/health-news/articles/2014/10/15/limiting-malpractice-claims-may-not-curb-costly-medical-tests"
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
X-Powered-By: Brightspot
Content-Type: text/html;charset=UTF-8
Date: Fri, 20 May 2016 06:45:12 GMT
Set-Cookie: JSESSIONID=A0C35776067AABCF9E029150C64D8D91; Path=/; HttpOnly
Transfer-Encoding: chunked
但是如果 你的 响应中缺少 header 那么这通常不是 Python 的错,当然也不是你的代码的错。
可能是您遇到了错误的服务器或临时故障,或者您联系的服务器出于某种原因不喜欢您。例如,您的示例响应 header 也将 content-length 设置为 0,表示根本没有可提供的内容。
给您该响应的服务器是 BigIP
,一个 load balancer / network router product from a company called F5。很难说到底是哪种(他们有全局路由服务器以及 per-datacenter 或集群负载平衡器)。可能是 back-end 服务器中的负载均衡器 运行 为请求提供服务,您所在地区没有服务器,或者负载均衡器认为您发送了太多请求并拒绝提供你不止这个反应,或者是月相不对,木星逆行而大发脾气。我们无从知晓!
但是,为了防止再次发生这种情况,请务必查看响应状态代码。它很可能是 4xx or 5xx 状态代码,表示您的请求或服务器出现问题。例如,429 状态代码响应表示您在短时间内发出了太多请求,应该放慢速度。通过检查 r.status_code
来测试它。
我已经 运行 这个查询之前与其他 URL 数百万(是的,数百万)次。但是,在检查以下网页的 content-type 时出现 KeyError。
代码片段:
r = requests.get("http://health.usnews.com/health-news/articles/2014/10/15/limiting-malpractice-claims-may-not-curb-costly-medical-tests", timeout=10, headers=headers)
if "text/html" in r.headers["content-type"]:
错误:
KeyError: 'content-type'
我查看了 r.headers
的内容,它是:
CaseInsensitiveDict({'date': 'Fri, 20 May 2016 06:44:19 GMT', 'content-length': '0', 'connection': 'keep-alive', 'server': 'BigIP'})
可能是什么原因造成的?
并非所有服务器都设置 Content-Type header。如果缺少默认值,请使用 .get()
检索默认值:
if "text/html" in r.headers.get("content-type", ''):
对于您提供的URL,我无法重现:
$ curl -s -D - -o /dev/null "http://health.usnews.com/health-news/articles/2014/10/15/limiting-malpractice-claims-may-not-curb-costly-medical-tests"
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
X-Powered-By: Brightspot
Content-Type: text/html;charset=UTF-8
Date: Fri, 20 May 2016 06:45:12 GMT
Set-Cookie: JSESSIONID=A0C35776067AABCF9E029150C64D8D91; Path=/; HttpOnly
Transfer-Encoding: chunked
但是如果 你的 响应中缺少 header 那么这通常不是 Python 的错,当然也不是你的代码的错。
可能是您遇到了错误的服务器或临时故障,或者您联系的服务器出于某种原因不喜欢您。例如,您的示例响应 header 也将 content-length 设置为 0,表示根本没有可提供的内容。
给您该响应的服务器是 BigIP
,一个 load balancer / network router product from a company called F5。很难说到底是哪种(他们有全局路由服务器以及 per-datacenter 或集群负载平衡器)。可能是 back-end 服务器中的负载均衡器 运行 为请求提供服务,您所在地区没有服务器,或者负载均衡器认为您发送了太多请求并拒绝提供你不止这个反应,或者是月相不对,木星逆行而大发脾气。我们无从知晓!
但是,为了防止再次发生这种情况,请务必查看响应状态代码。它很可能是 4xx or 5xx 状态代码,表示您的请求或服务器出现问题。例如,429 状态代码响应表示您在短时间内发出了太多请求,应该放慢速度。通过检查 r.status_code
来测试它。