如何使用requests库在Python爬虫中获取响应内容及状态码?
- 内容介绍
- 文章标签
- 相关推荐
本文共计1079个文字,预计阅读时间需要5分钟。
首先在程序中引入Requests模块:import requests
一、获取不同类型的响应内容在发送请求后,服务器会返回一个响应内容。Requests通常自动解码响应内容,但我们可以通过设置参数来控制解码行为。
1. 文本响应内容获取文本类型的响应内容:
pythonresponse=requests.get('http://example.com')text_content=response.text
这里,`response.text` 会返回响应内容的字符串形式。
首先在程序中引入Requests模块
import requests
一、获取不同类型的响应内容
在发送请求后,服务器会返回一个响应内容,而且requests通常会自动解码响应内容
1.文本响应内容
获取文本类型的响应内容
r = requests.get('www.baidu.com') r.text # 通过文本的形式获取响应内容
'<!DOCTYPE html>\r\n<!--STATUS OK--><html> <head><meta 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=news.baidu.com name=tj_trnews class=mnav>æ\x96°é\x97»</a> <a href=www.hao123.com name=tj_trhao123 class=mnav>hao123</a> <a href=map.baidu.com name=tj_trmap class=mnav>å\x9c°å\x9b¾</a> <a href=v.baidu.com name=tj_trvideo class=mnav>è§\x86é¢\x91</a> <a href=tieba.baidu.com name=tj_trtieba class=mnav>è′′å\x90§</a> <noscript> <a href=img.558idc.com/uploadfile/allimg/python/login.gif?login&tpl=mn&u=img.558idc.com/uploadfile/allimg/python/login.gif?login&tpl=mn&u=\'+ 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=home.baidu.com>å\x853äo\x8eç\x99¾åo|</a> <a href=ir.baidu.com>About Baidu</a> </p> <p id=cp>©2017 Baidu <a href=www.baidu.com/duty/>使ç\x94¨ç\x99¾åo|å\x89\x8då¿\x85èˉ»</a> <a href=img.558idc.com/uploadfile/allimg/python/error.html> </p> </div> </div> </div> </body> </html>\r\n'
通过encoding来获取响应内容的编码以及修改编码
r.encoding
'ISO-8859-1'
2.二进制响应内容
r.content # 通过content获取的内容便是二进制类型的
3.JSON响应内容
r.json()
4.原始响应内容
r = requests.get('www.baidu.com',stream=True) print(r.raw) # 就是urllib中的HTTPResponse对象 print(r.raw.read(10))
<requests.packages.urllib3.response.HTTPResponse object at 0x00000077940AEEF0> b'\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\x03'
二、响应状态码
获取响应状态码
r = requests.get('www.baidu.com') r.status_code
200
判断响应状态码
r.status_code == requests.codes.ok
True
当发送一个错误请求时,抛出异常
bad_r = requests.get('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'}
获取响应头的具体字段
print(r.headers['Server']) print(r.headers.get('Server'))
bfe/1.0.8.18 bfe/1.0.8.18
更多关于Python爬虫库requestsr的使用方法请查看下面的相关链接
本文共计1079个文字,预计阅读时间需要5分钟。
首先在程序中引入Requests模块:import requests
一、获取不同类型的响应内容在发送请求后,服务器会返回一个响应内容。Requests通常自动解码响应内容,但我们可以通过设置参数来控制解码行为。
1. 文本响应内容获取文本类型的响应内容:
pythonresponse=requests.get('http://example.com')text_content=response.text
这里,`response.text` 会返回响应内容的字符串形式。
首先在程序中引入Requests模块
import requests
一、获取不同类型的响应内容
在发送请求后,服务器会返回一个响应内容,而且requests通常会自动解码响应内容
1.文本响应内容
获取文本类型的响应内容
r = requests.get('www.baidu.com') r.text # 通过文本的形式获取响应内容
'<!DOCTYPE html>\r\n<!--STATUS OK--><html> <head><meta 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=news.baidu.com name=tj_trnews class=mnav>æ\x96°é\x97»</a> <a href=www.hao123.com name=tj_trhao123 class=mnav>hao123</a> <a href=map.baidu.com name=tj_trmap class=mnav>å\x9c°å\x9b¾</a> <a href=v.baidu.com name=tj_trvideo class=mnav>è§\x86é¢\x91</a> <a href=tieba.baidu.com name=tj_trtieba class=mnav>è′′å\x90§</a> <noscript> <a href=img.558idc.com/uploadfile/allimg/python/login.gif?login&tpl=mn&u=img.558idc.com/uploadfile/allimg/python/login.gif?login&tpl=mn&u=\'+ 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=home.baidu.com>å\x853äo\x8eç\x99¾åo|</a> <a href=ir.baidu.com>About Baidu</a> </p> <p id=cp>©2017 Baidu <a href=www.baidu.com/duty/>使ç\x94¨ç\x99¾åo|å\x89\x8då¿\x85èˉ»</a> <a href=img.558idc.com/uploadfile/allimg/python/error.html> </p> </div> </div> </div> </body> </html>\r\n'
通过encoding来获取响应内容的编码以及修改编码
r.encoding
'ISO-8859-1'
2.二进制响应内容
r.content # 通过content获取的内容便是二进制类型的
3.JSON响应内容
r.json()
4.原始响应内容
r = requests.get('www.baidu.com',stream=True) print(r.raw) # 就是urllib中的HTTPResponse对象 print(r.raw.read(10))
<requests.packages.urllib3.response.HTTPResponse object at 0x00000077940AEEF0> b'\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\x03'
二、响应状态码
获取响应状态码
r = requests.get('www.baidu.com') r.status_code
200
判断响应状态码
r.status_code == requests.codes.ok
True
当发送一个错误请求时,抛出异常
bad_r = requests.get('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'}
获取响应头的具体字段
print(r.headers['Server']) print(r.headers.get('Server'))
bfe/1.0.8.18 bfe/1.0.8.18
更多关于Python爬虫库requestsr的使用方法请查看下面的相关链接

