text与content的区别
requests对象的get和post⽅法都会返回⼀个Response对象,这个对象⾥⾯存的是服务器返回的所有信息,包括响应头,响应状态码等。其中返回的⽹页部分会存在.content和.text两个对象中。
两者区别在于,content中间存的是字节码,⽽text中存的是Beautifulsoup根据猜测的编码⽅式将content内容编码成字符串。
直接输出content,会发现前⾯存在b'这样的标志,这是字节字符串的标志,⽽text是,没有前⾯的b,对于纯ascii码,这两个可以说⼀模⼀样,对于其他的⽂字,需要正确编码才能正常显⽰。⼤部分情况建议使⽤.text,因为显⽰的是汉字,但有时会显⽰乱码,这时需要
⽤.content.decode('utf-8'),中⽂常⽤utf-8和GBK,GB2312等。这样可以⼿⼯选择⽂字编码⽅式。
所以简⽽⾔之,.text是现成的字符串,.content还要编码,但是.text不是所有时候显⽰都正常,这是就需要⽤.content进⾏⼿动编码。
可以举⼏个例⼦看看:
import requests
from bs4 import BeautifulSoup
response = ('www.baidu')
re_text =
re_content = t
print (re_text)
print (type(re_text))
print (re_content)
print (type(re_content))
position和location的区别
数据库的分页语法
re_text =
print (re_text)
输出结果为:(下⾯的分割线是⼿动加的)
<!DOCTYPE html>
<!--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=ss1.bdstatic/5eN1bjq8AAUYm2zgoY3K/r/www/cache/bdorz/baidu.min.css><title>ç¾åº¦ä¸ä¸ï¼ä½ å°±ç¥é</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/img/bd_logo1.png width=270 height=129> </div> <form id=form name=f action=//www.baidu/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=ç¾åº¦ä¸ä¸ class="bg s_btn" autofocus></span> </form> </div> </div> <div id=u1> <a href=news.baidu name=tj_trnews
class=mnav>æ°é»</a> <a href=www.hao123 name=tj_trhao123 class=mnav>hao123</a> <a href=map.baidu name=tj_trmap class=mnav>å°å¾</a> <a href=v.baidu name=tj_trvideo class=mnav>è§é¢</a> <a href=tieba.baidu name=tj_trtieba class=mnav>è´´å§</a> <noscript> <a href=www.baidu/bdorz/login.gif?login&tpl=mn&u=http%3A%2F%2Fwww.baidu%2f%3fbdorz_come%3d1 name=tj_login class=lb>ç»å½</a> </noscript> <script>document.write('<a href="www.baidu/bdorz/login.gif?login&tpl=mn&u='+ encodeURIComponent(window.location.href+ (window.location.search === "" ? "?" : "&")+ "bdorz_come=1")+ '" name="tj_login" class="lb">ç»å½</a>');                </script> <a href=//www.baidu/more/ name=tj_briicon class=bri >æ´å¤äº§å</a> </div> </div> </div> <div id=ftCon> <div id=ftConw> <p id=lh> <a href=home.baidu>å³äºç¾åº¦</a> <a href=ir.baidu>About Baidu</a> </p> <p
id=cp>©2017 Baidu <a href=www.baidu/duty/>使ç¨ç¾åº¦åå¿è¯»</a>  <a href=jian
yi.baidu/ class=cp-feedback>æè§åé¦</a> äº¬ICPè¯030173å·  <img src=//www.baidu/img/gs.gif> </p> </div> </div> </div> </body> </html>
<class 'str'>
b'<!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=ss1.bdstatic/5eN1bjq8AAUYm2zgoY3K/r/www/cache/bdorz/baidu.min.css>
<title>\xe7\x99\xbe\xe5\xba\xa6\xe4\xb8\x80\xe4\xb8\x8b\xef\xbc\x8c\xe4\xbd\xa0\xe5\xb0\xb1\xe7\x9f\xa5\xe9\x81\x93</title></head> <body
js转换视频格式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/img/bd_logo1.png width=270 height=129> </div> <form id=form name=f action=//www.baidu/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=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=\xe7\x99\xbe\xe5\xba\xa6\xe4\xb8\x80\xe4\xb8\x8b class="bg s_btn" autofocus></span> </form> </div> </div> <div id=u1> <a
href=news.baidu name=tj_trnews class=mnav>\xe6\x96\xb0\xe9\x97\xbb</a> <a href=www.hao123 name=tj_trhao123
class=mnav>hao123</a> <a href=map.baidu name=tj_trmap class=mnav>\xe5\x9c\xb0\xe5\x9b\xbe</a> <a href=v.baidu
name=tj_trvideo class=mnav>\xe8\xa7\x86\xe9\xa2\x91</a> <a href=tieba.baidu name=tj_trtieba class=mnav>\xe8\xb4\xb4\xe5\x90\xa7</a> <noscript> <a href=www.baidu/bdorz/login.gif?login&tpl=mn&u=http%3A%2F%2Fwww.baidu%2f%3fbdorz_come%3d1
name=tj_login class=lb>\xe7\x99\xbb\xe5\xbd\x95</a> </noscript> <script>document.write(\'<a href="www.baidu/bdorz/login.gif?
login&tpl=mn&u=\'+ encodeURIComponent(window.location.href+ (window.location.search === "" ? "?" : "&")+ "bdorz_come=1")+ \'" name="tj_login" class="lb">\xe7\x99\xbb\xe5\xbd\x95</a>\');\r\n                </script> <a href=//www.baidu/more/ name=tj_briicon class=bri >\xe6\x9b\xb4\xe5\xa4\x9a\xe4\xba\xa7\xe5\x93\x81</a> </div> </div> </div> <div id=ftCon> <div id=ftConw> <p id=lh> <a
href=home.baidu>\xe5\x85\xb3\xe4\xba\x8e\xe7\x99\xbe\xe5\xba\xa6</a> <a href=ir.baidu>About Baidu</a> </p> <p
id=cp>©2017 Baidu <a
href=www.baidu/duty/>\xe4\xbd\xbf\xe7\x94\xa8\xe7\x99\xbe\xe5\xba\xa6\xe5\x89\x8d\xe5\xbf\x85\xe8\xaf\xbb</a>  <a
href=jianyi.baidu/ class=cp-
easyui登录feedback>\xe6\x84\x8f\xe8\xa7\x81\xe5\x8f\x8d\xe9\xa6\x88</a> \xe4\xba\xacICP\xe8\xaf\x81030173\xe5\x8f\xb7 
<imgfut webapp
src=//www.baidu/img/gs.gif> </p> </div> </div> </div> </body> </html>\r\n'
<class 'bytes'>
<!DOCTYPE html>
<!--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=ss1.bdstatic/5eN1bjq8AAUYm2zgoY3K/r/www/cache/bdorz/baidu.min.css><title>百度⼀下,你就知道</title></head> <body
php7垃圾回收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/img/bd_logo1.png width=270 height=129> </div> <form id=form name=f action=//www.baidu/s class=fm>
<input type=hidden name=bdorz_come value=1> <input type=hidden name=ie value=utf-8> <input ty
pe=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=百度⼀下 class="bg s_btn" autofocus></span> </form> </div> </div> <div id=u1> <a href=news.baidu name=tj_trnews class=mnav>新闻</a> <a href=www.hao123 name=tj_trhao123 class=mnav>hao123</a> <a href=map.baidu name=tj_trmap class=mnav>地图</a> <a href=v.baidu name=tj_trvideo class=mnav>视频</a> <a href=tieba.baidu name=tj_trtieba class=mnav>贴吧</a> <noscript> <a href=www.baidu/bdorz/login.gif?login&tpl=mn&u=http%3A%2F%2Fwww.baidu%2f%3fbdorz_come%3d1 name=tj_login
class=lb>登录</a> </noscript> <script>document.write('<a href="www.baidu/bdorz/login.gif?login&tpl=mn&u='+
encodeURIComponent(window.location.href+ (window.location.search === "" ? "?" : "&")+ "bdorz_come=1")+ '" name="tj_login" class="lb">登录</a>');
</script> <a href=//www.baidu/more/ name=tj_briicon class=bri >更多产品
</a> </div> </div> </div> <div id=ftCon> <div id=ftConw> <p id=lh> <a href=home.baidu>关于百度</a> <a href=ir.baidu>About Baidu</a> </p> <p
id=cp>©2017 Baidu <a href=www.baidu/duty/>使⽤百度前必读</a>  <a href=jianyi.baidu/ class=cp-feedback>意见反馈</a> ;京ICP证030173号  <img src=//www.baidu/img/gs.gif> </p> </div> </div> </div> </body> </html>

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。