您的位置: 网站首页> requests爬虫> 当前文章

requests获取状态码与http响应头、请求头

老董-我爱我家房产SEO2019-07-18175围观,109赞

  日常生活中有时候打开一个网页提示404或者500错误,这种404或者500就是一种状态码,代表一种含义来表达这次访问网页的结果咋样。

  访问一个网页就是向服务器发1个请求,实际上在浏览器接收并显示网页前,网页所在的服务器会返回一个响应头(server header),响应头信息里包含1个HTTP状态码,不同的状态码的值代表不同的含义。

  常见的HTTP状态码:

  200 - 请求成功

  301 - 资源(网页等)被永久转移到其它URL

  403 - 禁止此请求

  404 - 请求的资源不存在

  500 - 内部服务器错误

  我们可以通过浏览器或者其他专业的抓包工具(比如fiddler)来查看响应头,随便打开1个网页可以正常访问就是200。

  在requests请求网页的相应对象里面有个status_code属性可以查看状态码。

# -*- coding: utf-8 -*-

r = requests.get("https://www.baidu.com/")
print(r.status_code)

r = requests.get("https://www.baidu.com/python66.com")
print(r.status_code)

200
404

  此外,Requests还附带了一个内置的状态码查询对象requests.codes.ok,不过这个东西尽量少用,因为他的意义并不是想当然的理解的只有200状态码才正确的输出,如果是404请求requests.codes.ok返回也是200。

# -*- coding: utf-8 -*-

r = requests.get("https://www.baidu.com/")
print(requests.codes.ok)

r = requests.get("https://www.baidu.com/python66.com") # 404
print(requests.codes.ok)

200
200

  如果发送了一个错误请求(一个 4XX 客户端错误,或者 5XX 服务器错误响应),我们可以通过Response.raise_for_status()来抛出异常

# -*- coding: utf-8 -*-

r = requests.get("https://www.baidu.com/")
print(r.raise_for_status()) #200输出None

r = requests.get("https://www.baidu.com/python66.com") # 404
print(r.raise_for_status())

None
requests.exceptions.HTTPError: 404 Client Error: Not Found for url: https://www.baidu.com/python66.com

  完整响应头

  状态码属于响应头的一部分,要获取全部响应头也非常简单,直接r.headers就可以查看以一个Python字典形式展示的服务器响应头。

# -*- coding: utf-8 -*-
headers = {
'user-agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36 Edg/95.0.1020.44',
}

url = 'https://www.baidu.com'
r = requests.get(url,headers=headers)
print(r.headers)
{'Bdpagetype': '1', 'Bdqid': '0xdee12fd40007efc5', 'Cache-Control': 'private', 'Connection': 'keep-alive', 'Content-Encoding': 'gzip', 'Content-Type': 'text/html;charset=utf-8', 'Date': 'Mon, 15 Nov 2021 05:15:44 GMT', 'Expires': 'Mon, 15 Nov 2021 05:15:02 GMT', 'P3p': 'CP=" OTI DSP COR IVA OUR IND COM ", CP=" OTI DSP COR IVA OUR IND COM "', 'Server': 'BWS/1.1', 'Set-Cookie': 'BAIDUID=237ABB0DE68314DD3DBDA5546CCD1E67:FG=1; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com, BIDUPSID=237ABB0DE68314DD3DBDA5546CCD1E67; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com, PSTM=1636953344; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com, BAIDUID=237ABB0DE68314DD0A4420143BA12478:FG=1; max-age=31536000; expires=Tue, 15-Nov-22 05:15:44 GMT; domain=.baidu.com; path=/; version=1; comment=bd, BDSVRTM=0; path=/, BD_HOME=1; path=/, H_PS_PSSID=34942_34443_35105_31660_35049_35097_34584_34518_34917_34606_34815_26350_34973_34866_35145_35018; path=/; domain=.baidu.com', 'Strict-Transport-Security': 'max-age=172800', 'Traceid': '1636953344023825255416060170333759991749', 'X-Frame-Options': 'sameorigin', 'X-Ua-Compatible': 'IE=Edge,chrome=1', 'Transfer-Encoding': 'chunked'}

  HTTP头部是大小写不敏感的,因此可以使用任意大写形式来访问这些响应头字段。

# -*- coding: utf-8 -*-
headers = {
'user-agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36 Edg/95.0.1020.44',
}

url = 'https://www.baidu.com'
r = requests.get(url,headers=headers)
print(r.headers['Connection'])
print(r.headers['connection'])
keep-alive
keep-alive

  如果想得到发送到服务器的请求头,可以通过如下方法获取

r.request.headers

很赞哦!

python编程网提示:转载请注明来源www.python66.com。
有宝贵意见可添加站长微信(底部),获取技术资料请到公众号(底部)。同行交流请加群 python学习会

文章评论

    requests获取状态码与http响应头、请求头文章写得不错,值得赞赏

站点信息

  • 网站程序:Laravel
  • 客服微信:a772483200