简体   繁体   English

在某些请求的标头中,内容类型为空白

[英]Content-type is blank in the headers of some requests

I've ran this queries millions (yes, millions) of times before with other URLs. 我已经使用其他URL进行过数百万次(是,数百万次)查询。 However, I'm getting a KeyError when checking the content-type of the following webpage. 但是,检查以下网页的内容类型时出现KeyError。

Code snippet: 程式码片段:

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"]:

Error: 错误:

KeyError: 'content-type'

I checked the content of r.headers and it's: 我检查了r.headers的内容,它是:

CaseInsensitiveDict({'date': 'Fri, 20 May 2016 06:44:19 GMT', 'content-length': '0', 'connection': 'keep-alive', 'server': 'BigIP'})

What could be causing this? 是什么原因造成的?

Not all servers set a Content-Type header. 并非所有服务器都设置Content-Type标头。 Use .get() to retrieve a default if it is missing: 如果缺少默认值,请使用.get()检索默认值:

if "text/html" in r.headers.get("content-type", ''):

For the URL you gave I can't reproduce this: 对于您提供的网址,我无法重现:

$ 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

but if the header is missing from your response then it usually isn't Python's fault, and certainly not your code's fault. 但如果标题是从您的响应丢失的话,通常是不能Python的错,更不是你的代码的错。

It could be you encountered a buggy server or temporary glitch, or the server you contacted doesn't like you for one reason or another. 可能是您遇到了服务器故障或临时故障,或者您联系的服务器由于某种原因而不喜欢您。 Your sample response headers have the content-length set to 0 as well, for example, indicating there was no content to serve at all. 您的样本响应标头的content-length也设置为0,例如,表明根本没有要提供的内容。

The server that gave you that response is BigIP , a load balancer / network router product from a company called F5 . 做出响应的服务器是BigIP ,这是一家名为F5的公司负载平衡器/网络路由器产品 Hard to say exactly what kind (they have global routing servers as well as per-datacenter or cluster load balancers). 很难说到底是哪种类型(它们具有全局路由服务器以及每个数据中心或群集负载平衡器)。 It could be that the load balancer ran out of back-end servers to serve the request, doesn't have servers in your region, or the load balancer decided that you are sending too many requests and refuses to give you more than just this response, or it is the wrong phase of the moon and Jupiter is in retrograde and it threw a tantrum. 可能是负载平衡器用完了后端服务器来满足请求,或者您所在的区域中没有服务器,或者负载平衡器决定您发送的请求太多,并且拒绝仅向您提供此响应,或者它是错误的月相,而木星处于逆行并发脾气。 We can't know! 我们不知道!

But, just in case this happens again, do also look at the response status code. 但是,以防万一再次发生这种情况,请务必查看响应状态代码。 It may well be a 4xx or 5xx status code indicating that something was wrong with your request or with the server. 可能是4xx5xx状态代码,表明您的请求或服务器出了点问题。 For example, a 429 status code response would indicate you made too many requests in a short amount of time and should slow down. 例如,一个429状态码响应将指示您在短时间内提出了太多请求,并且应放慢速度。 Test for it by checking r.status_code . 通过检查r.status_code测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM