python + get requests-- request without parameters

Import   Requests 

URL = " http://www.baidu.com "     # request URL 

yctime = 0.05             # can not exceed the time 

RESP = requests.get (URL, timeout = yctime) 







Print ( " # -------- -------------------------------------------------- 1 ------------------------------------- " ) 

Print (of the type (resp.url))   # rul a return type, it returns a string description: <class 'STR'> 

Print (resp.url)    # access URL: HTTP: //www.baidu.com/ 








Print ( "# ------------------------------------------------- 2 ---------------------------------------------- " ) 

Print (type (resp.history))    # returns a list of the type: <class 'list'> 

Print (resp.history)      # returns a null [], and described the history is not redirected printing request (in the form of a list show) 








Print ( " # -------------------------------------------- -------------------------------------------------- -3 " ) 

Print (type (resp.status_code))    # returns an integer type: <class 'int'> 

Print (resp.status_code)    #   return a status code: 200 determines whether access to normal 







Print ( "# ------------------------------------------------- 4 ---------------------------------------------- " ) 

IF (resp.status_code == requests.codes.ok):
     Print ( " the request was successful " )
 the else :
     Print ( " failed " )                    # returns: the request is successful 


IF (resp.status_code == 200 ):
     Print ( " the request was successful " )
 the else :
     print ( " failed " )                   # returns: the request was successful 









print( " # ----------------------------------------------- ------------------------------------------------ 5 " ) 

Print (resp.apparent_encoding)     # returns: utf-8 content encoding an analysis of the response (alternative encoding) contents from a 

Print (resp.encoding)                     # returns the current encoding formats: ISO-8859-1 # HTTP from guessing response content header encoding 

resp.encoding = ' UTF-. 8 '                  # set encoding format # response.encoding = "utf-8" or response.encoding = "gbk" specifically see pages encoded in what way 
                                        # if normal show, do not set the encoding format, the normal output 

Print (resp.encoding)                     # returns: Previous setting success utf-8 Description 


Print (resp.content)          #Returns bytes of binary data type, if you want to take pictures, files, you can response.content 

# changes encoded using response.content.deocde ( "utf8") 

# : return data type 

# : response.text returns a unicode text data type 

# : response.content returns bytes of binary data type 

# : that if you want to take the text data can be response.text If you want to take pictures, files, you can response.content 

# : the Response .text returns Unicode format, usually need to be converted to utf-8 format, or is garbled. 
# 
#   Response.content binary mode, you can download the video and the like, if want to see it as a need to decode utf-8 format. 

# : Either through response.content.decode ( "utf-8) or by way response.encoding =" utf-8 "are a way to avoid garbled problem occurs 


# : response.apparent_encoding analysis of the response from the content content encoding (encoding alternatively) 









Print ( "#-----------------------------------------------------------------------------------------------6")

#{'Cache-Control': 'private, no-cache, no-store, proxy-revalidate, no-transform', 'Connection': 'keep-alive',
# 'Content-Encoding': 'gzip', 'Content-Type': 'text/html', 'Date': 'Fri, 21 Feb 2020 17:06:24 GMT',
# 'Last-Modified': 'Mon, 23 Jan 2017 13:28:23 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(resp.headers)                'Content-encoding'(resp.headers [Printreturns the response header shown above#

])         # Returns: the gzip 

Print (resp.headers [ ' the Content-type ' ])             # Returns: text / html 

Print (resp.headers.get ( '
the Content-type ')) # Returns: text / html
 
 


Print (resp.headers [ ' DATE ' ])             # Returns: Fri, 21 is On Feb 2020 17:06:24 GMT 


Print ( " # --------------------- -------------------------------------------------- . 7 ------------------------ " ) 

# <RequestsCookieJar [<= 27315 for cookies BDORZ .baidu.com />]> 


Print (resp.cookies )                  # returns a cookie, particularly as 

# Print (resp.cookies [ 'name']) # setting the name parameter, to access to a specific value 


Print ( " # -------------- -------------------------------------------------- 8 ------------------------------- " ) 

Print(type (resp.text))            # Returns a str type: <class 'str'> 

Print (resp.text)                 # returns the requested response body

 

 

Results of the:

 

# ------------------------------------------------- . 1 ----------------------------------------------
<class' STR '>
http://www.baidu.com/
# ------------------------------------ -------------------------------------------------- 2 ---------
<class 'List'>
[]
# ------------------------------- -------------------------------------------------- . 3 --------------
<class 'int'>
200 is
# --------------------------- -------------------------------------------------- ------------------ 4
request succeeds
request is successful
# -------------------------- -------------------------------------------------- . 5 -------------------
UTF. 8-
the ISO-8859-1
UTF-. 8
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=http://s1.bdstatic.com/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 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></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"></span> </form> </div> </div> <div id=u1> <a href=http://news.baidu.com name=tj_trnews class="mnav">\xe6\x96\xb0\xe9\x97\xbb</a> <a href=http://www.hao123.com name=tj_trhao123 class="mnav">hao123</a> <a href=http://map.baidu.com name=tj_trmap class="mnav">\xe5\x9c\xb0\xe5\x9b\xbe</a> <a href=http://v.baidu.com name=tj_trvideo class="mnav">\xe8\xa7\x86\xe9\xa2\x91</a> <a href=http://tieba.baidu.com name=tj_trtieba class="mnav">\xe8\xb4\xb4\xe5\x90\xa7</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">\xe7\x99\xbb\xe5\xbd\x95</a> </noscript> <script>document.write(\'<a href="http://www.baidu.com/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>\');</script> <a href=//www.baidu.com/more/ name=tj_briicon class="bri" style="display: block;">\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=http://home.baidu.com>\xe5\x85\xb3\xe4\xba\x8e\xe7\x99\xbe\xe5\xba\xa6</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/>\xe4\xbd\xbf\xe7\x94\xa8\xe7\x99\xbe\xe5\xba\xa6\xe5\x89\x8d\xe5\xbf\x85\xe8\xaf\xbb</a>&nbsp; <a href=http://jianyi.baidu.com/ class="cp-feedback">\xe6\x84\x8f\xe8\xa7\x81\xe5\x8f\x8d\xe9\xa6\x88</a>&nbsp;\xe4\xba\xacICP\xe8\xaf\x81030173\xe5\x8f\xb7&nbsp; <img src=//www.baidu.com/img/gs.gif> </p> </div> </div> </div> </body> </html>\r\n'
#-----------------------------------------------------------------------------------------------6
{'Cache-Control': 'private, no-cache, no-store, proxy-revalidate, no-transform', 'Connection': 'keep-alive', 'Content-Encoding': 'gzip', 'Content-Type': 'text/html', 'Date': 'Fri, 21 Feb 2020 17:15:21 GMT', 'Last-Modified': 'Mon, 23 Jan 2017 13:28:24 GMT', 'Pragma': 'no-cache', 'Server': 'bfe/1.0.8.18', 'Set-Cookie': 'BDORZ=27315; max-age=86400; domain=.baidu.com; path=/', 'Transfer-Encoding': 'chunked'}
gzip
text/html
Fri, 21 Feb 2020 17:15:21 GMT
#-----------------------------------------------------------------------------------------------7
<RequestsCookieJar[<Cookie BDORZ=27315 for .baidu.com/>]>
#-----------------------------------------------------------------------------------------------8
<class 'str'>
<!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=http://s1.bdstatic.com/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.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></span><span class="bg s_btn_wr"><input type=submit id=su value=百度一下 class="bg s_btn"></span> </form> </div> </div> <div id=u1> <a href=http://news.baidu.com name=tj_trnews class="mnav">新闻</a> <a href=http://www.hao123.com name=tj_trhao123 class="mnav">hao123</a> <a href=http://map.baidu.com name=tj_trmap class="mnav">地图</a> <a href=http://v.baidu.com name=tj_trvideo class="mnav">视频</a> <a href=http://tieba.baidu.com name=tj_trtieba class="mnav">贴吧</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">登录</a> </noscript> <script>document.write('<a href="http://www.baidu.com/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.com/more/ name=tj_briicon class="bri" style="display: block;">更多产品</a> </div> </div> </div> <div id=ftCon> <div id=ftConw> <p id=lh> <a href=http://home.baidu.com>关于百度</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/>使用百度前必读</a>&nbsp; <a href=http://jianyi.baidu.com/ class="cp-feedback">意见反馈</a>&nbsp;京ICP证030173号&nbsp; <img src=//www.baidu.com/img/gs.gif> </p> </div> </div> </div> </body> </html>

 

Guess you like

Origin www.cnblogs.com/xiaobaibailongma/p/12343932.html