欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python爬蟲(chóng)庫(kù)requests獲取響應(yīng)內(nèi)容、響應(yīng)狀態(tài)碼、響應(yīng)頭

 更新時(shí)間:2020年01月25日 13:46:12   作者:BQW_  
上一節(jié)我們給大家介紹了Python爬蟲(chóng)庫(kù)requests的發(fā)送請(qǐng)求傳參等使用方法,今天為大家介紹下requests獲取響應(yīng)內(nèi)容、響應(yīng)狀態(tài)碼、響應(yīng)頭等相關(guān)信息

首先在程序中引入Requests模塊

import requests

一、獲取不同類(lèi)型的響應(yīng)內(nèi)容

在發(fā)送請(qǐng)求后,服務(wù)器會(huì)返回一個(gè)響應(yīng)內(nèi)容,而且requests通常會(huì)自動(dòng)解碼響應(yīng)內(nèi)容

1.文本響應(yīng)內(nèi)容

獲取文本類(lèi)型的響應(yīng)內(nèi)容

r = requests.get('https://www.baidu.com')
r.text # 通過(guò)文本的形式獲取響應(yīng)內(nèi)容
'<!DOCTYPE html>\r\n<!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=https://ss1.bdstatic.com/5eN1bjq8AAUYm2zgoY3K/r/www/cache/bdorz/baidu.min.css><title>ç\x99¾åo|ä¸\x80ä¸\x8bï¼\x8cä½\xa0å°±ç\x9f¥é\x81\x93</title></head> <body link=#0000cc> <div id=wrapper> <div id=head> <div class=head_wrapper> <div class=s_form> <div class=s_form_wrapper> <div id=lg> <img hidefocus=true src=//www.baidu.com/img/bd_logo1.png width=270 height=129> </div> <form id=form name=f action=//www.baidu.com/s class=fm> <input type=hidden name=bdorz_come value=1> <input type=hidden name=ie value=utf-8> <input type=hidden name=f value=8> <input type=hidden name=rsv_bp value=1> <input type=hidden name=rsv_idx value=1> <input type=hidden name=tn value=baidu><span class="bg s_ipt_wr"><input id=kw name=wd class=s_ipt value maxlength=255 autocomplete=off autofocus=autofocus></span><span class="bg s_btn_wr"><input type=submit id=su value=ç\x99¾åo|ä¸\x80ä¸\x8b class="bg s_btn" autofocus></span> </form> </div> </div> <div id=u1> <a href=http://news.baidu.com name=tj_trnews class=mnav>æ\x96°é\x97»</a> <a href=https://www.hao123.com name=tj_trhao123 class=mnav>hao123</a> <a href=http://map.baidu.com name=tj_trmap class=mnav>å\x9c°å\x9b¾</a> <a href=http://v.baidu.com name=tj_trvideo class=mnav>è§\x86é¢\x91</a> <a href=http://tieba.baidu.com name=tj_trtieba class=mnav>è′′å\x90§</a> <noscript> <a href=http://www.baidu.com/bdorz/login.gif?login&amp;tpl=mn&amp;u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1 name=tj_login class=lb>ç\x99»å½\x95</a> </noscript> <script>document.write(\'<a + encodeURIComponent(window.location.href+ (window.location.search === " rel="external nofollow" " ? "?" : "&")+ "bdorz_come=1")+ \'" name="tj_login" class="lb">ç\x99»å½\x95</a>\');\r\n        </script> <a href=//www.baidu.com/more/ name=tj_briicon class=bri style="display: block;">æ\x9b′å¤\x9aäo§å\x93\x81</a> </div> </div> </div> <div id=ftCon> <div id=ftConw> <p id=lh> <a href=http://home.baidu.com>å\x853äo\x8eç\x99¾åo|</a> <a href=http://ir.baidu.com>About Baidu</a> </p> <p id=cp>&copy;2017&nbsp;Baidu&nbsp;<a href=http://www.baidu.com/duty/>使ç\x94¨ç\x99¾åo|å\x89\x8då¿\x85èˉ»</a>&nbsp; <a href=http://jianyi.baidu.com/ class=cp-feedback>æ\x84\x8fè§\x81å\x8f\x8dé|\x88</a>&nbsp;äo¬ICPèˉ\x81030173å\x8f·&nbsp; <img src=//www.baidu.com/img/gs.gif> </p> </div> </div> </div> </body> </html>\r\n'

通過(guò)encoding來(lái)獲取響應(yīng)內(nèi)容的編碼以及修改編碼

r.encoding
'ISO-8859-1'

2.二進(jìn)制響應(yīng)內(nèi)容

r.content # 通過(guò)content獲取的內(nèi)容便是二進(jìn)制類(lèi)型的

3.JSON響應(yīng)內(nèi)容

r.json()

4.原始響應(yīng)內(nèi)容

r = requests.get('https://www.baidu.com',stream=True)
print(r.raw) # 就是urllib中的HTTPResponse對(duì)象
print(r.raw.read(10))
<requests.packages.urllib3.response.HTTPResponse object at 0x00000077940AEEF0>
b'\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\x03'

二、響應(yīng)狀態(tài)碼

獲取響應(yīng)狀態(tài)碼

r = requests.get('https://www.baidu.com')
r.status_code
200

判斷響應(yīng)狀態(tài)碼

r.status_code == requests.codes.ok
True

當(dāng)發(fā)送一個(gè)錯(cuò)誤請(qǐng)求時(shí),拋出異常

bad_r = requests.get('http://httpbin.org/status/404')
print(bad_r.status_code)
bad_r.raise_for_status()
404



---------------------------------------------------------------------------

HTTPError                 Traceback (most recent call last)

<ipython-input-15-9b812f4c5860> in <module>()
   1 bad_r = requests.get('http://httpbin.org/status/404')
   2 print(bad_r.status_code)
----> 3 bad_r.raise_for_status()


D:\Anaconda3\lib\site-packages\requests\models.py in raise_for_status(self)
  926 
  927     if http_error_msg:
--> 928       raise HTTPError(http_error_msg, response=self)
  929 
  930   def close(self):


HTTPError: 404 Client Error: NOT FOUND for url: http://httpbin.org/status/404

三、響應(yīng)頭

獲取響應(yīng)頭

r = requests.get('https://www.baidu.com')
r.headers
{'Cache-Control': 'private, no-cache, no-store, proxy-revalidate, no-transform', 'Connection': 'Keep-Alive', 'Content-Encoding': 'gzip', 'Content-Type': 'text/html', 'Date': 'Mon, 23 Jul 2018 09:04:12 GMT', 'Last-Modified': 'Mon, 23 Jan 2017 13:23:51 GMT', 'Pragma': 'no-cache', 'Server': 'bfe/1.0.8.18', 'Set-Cookie': 'BDORZ=27315; max-age=86400; domain=.baidu.com; path=/', 'Transfer-Encoding': 'chunked'}

獲取響應(yīng)頭的具體字段

print(r.headers['Server'])
print(r.headers.get('Server'))
bfe/1.0.8.18
bfe/1.0.8.18

更多關(guān)于Python爬蟲(chóng)庫(kù)requestsr的使用方法請(qǐng)查看下面的相關(guān)鏈接

相關(guān)文章

  • 詳解Python中的靜態(tài)方法與類(lèi)成員方法

    詳解Python中的靜態(tài)方法與類(lèi)成員方法

    這篇文章主要介紹了關(guān)于Python中靜態(tài)方法與類(lèi)成員的相關(guān)資料,文中通過(guò)示例代碼給大家詳細(xì)總結(jié)了兩者在語(yǔ)法和使用上的區(qū)別,有需要的朋友可以參考借鑒,下面來(lái)一起看看吧。
    2017-02-02
  • Python裝飾器模式定義與用法分析

    Python裝飾器模式定義與用法分析

    這篇文章主要介紹了Python裝飾器模式定義與用法,結(jié)合實(shí)例形式分析了Python裝飾器模式的具體定義、使用方法及相關(guān)操作技巧,需要的朋友可以參考下
    2018-08-08
  • 在樹(shù)莓派2或樹(shù)莓派B+上安裝Python和OpenCV的教程

    在樹(shù)莓派2或樹(shù)莓派B+上安裝Python和OpenCV的教程

    這篇文章主要介紹了在樹(shù)莓派2或樹(shù)莓派B+上安裝Python和OpenCV的教程,主要基于GTK庫(kù),并以Python2.7和OpenCV 2.4.X版本的安裝作為示例,需要的朋友可以參考下
    2015-03-03
  • python讀取csv文件示例(python操作csv)

    python讀取csv文件示例(python操作csv)

    這篇文章主要介紹了python讀取csv文件示例,這個(gè)示例簡(jiǎn)單說(shuō)明了一下python操作csv的方法,需要的朋友可以參考下
    2014-03-03
  • Micropython固件使用Pico刷固件并配置VsCode開(kāi)發(fā)環(huán)境的方法

    Micropython固件使用Pico刷固件并配置VsCode開(kāi)發(fā)環(huán)境的方法

    這篇文章主要介紹了Micropython固件使用Pico刷固件并配置VsCode開(kāi)發(fā)環(huán)境的方法,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2021-07-07
  • 實(shí)例說(shuō)明Python中比較運(yùn)算符的使用

    實(shí)例說(shuō)明Python中比較運(yùn)算符的使用

    這篇文章主要介紹了=Python中比較運(yùn)算符的使用,是Python學(xué)習(xí)當(dāng)中的基本知識(shí),需要的朋友可以參考下
    2015-05-05
  • Python中sys模塊常用方法與變量實(shí)例探究

    Python中sys模塊常用方法與變量實(shí)例探究

    sys?模塊是 Python 標(biāo)準(zhǔn)庫(kù)中的一個(gè)核心模塊,提供了與解釋器進(jìn)行交互的功能,了解?sys?模塊的方法和變量對(duì)于更有效地管理和調(diào)試 Python 程序至關(guān)重要,本文將深入探討?sys?模塊的常用方法和變量,通過(guò)詳細(xì)的示例代碼,幫助大家更全面地了解并靈活運(yùn)用這一關(guān)鍵模塊
    2024-01-01
  • 詳談Python基礎(chǔ)之內(nèi)置函數(shù)和遞歸

    詳談Python基礎(chǔ)之內(nèi)置函數(shù)和遞歸

    下面小編就為大家?guī)?lái)一篇Python基礎(chǔ)之內(nèi)置函數(shù)和遞歸。小編覺(jué)得挺不錯(cuò)的?,F(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-06-06
  • python標(biāo)準(zhǔn)庫(kù) datetime的astimezone設(shè)置時(shí)區(qū)遇到的坑及解決

    python標(biāo)準(zhǔn)庫(kù) datetime的astimezone設(shè)置時(shí)區(qū)遇到的坑及解決

    這篇文章主要介紹了python標(biāo)準(zhǔn)庫(kù) datetime的astimezone設(shè)置時(shí)區(qū)遇到的坑及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-09-09
  • Scrapy使用的基本流程與實(shí)例講解

    Scrapy使用的基本流程與實(shí)例講解

    今天小編就為大家分享一篇關(guān)于Scrapy使用的基本流程與實(shí)例講解,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧
    2018-10-10

最新評(píng)論