Network requests requess basic library use ---------- python reptile learning

Library installation requests:
Use cmd command line input pip install requests
When you install python add python to path is not checked, it will lead to error installation requests, we can create a path at the windows of the environment variable, add the path at the location pip.
You can refer to the article: https://blog.csdn.net/bimo123/article/details/89295896
 
request transmission request
url="http://www.baidu.com" 
import requests 
rep=requests.get(url) 
rep.text
'<!DOCTYPE html>\r\n<!‐‐STATUS OK‐‐><html> <head><meta http‐ equiv=content‐type content=text/html;charset=utf‐8><meta http‐equiv=X‐UA‐Co mpatible content=IE=Edge><meta content=always name=referrer><link rel=style sheet type=text/css href=http://s1.bdstatic.com/r/www/cache/bdorz/baidu.min.css><title>ç\x99¾åº ¦ä¸\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 sr c=//www.baidu.com/img/bd_logo1.png wid#....此处省略
What happened in the way requests in the request on the use of reuquest. Manner can get / post / put .....
 
In the requests for return data repository operation
 
Reads return data: text attributes, content attributes. In which the text is read property guess the way to read the string, so the data does not decode may occur, we can set the content property of decoding by the way.
 
content return data type is bytes, where we used decode, the decoding by the encoding mode depending on the page, the general coding method at page charset attribute meta tag
 
example:
= URL "http://www.baidu.com" 
REP = requests.get (URL) 

# use the text data acquired 
rep.text 
... front omitted <title> ç \ x99¾åº|ä¸ \ x80ä¸ \ x8bï¼ \ x8cä½ \ [deg.] xa0å ± ç \ x9F ¥ é \ X81 \ X93 </ Le TIT> </ head> <body 0000CC Link # => 

 # acquired data using the content and decodes 
rep.content.decode ( 'UTF-. 8') 
 . front omitted .. <title> Baidu, you know <head div id => </ title> </ head> <body link = # 0000cc> <d iv id = wrapper> <div class = head_wrapper> <div class = s_form> <d iv class = s_form_wrapper> <div id = lg> <img hidefocus = true src = // www.baidu.co m / img / bd_logo1.png width = 270 height = 129> </ div>

 

View encoding: encoding property

url="http://www.baidu.com" 
rep=requests.get(url) 
rep.encoding 
'ISO‐8859‐1'
For ISO-8859-1 decoding method we should use utf-8

 

See response status codes: status_code Properties
url="http://www.baidu.com
rep=requests.get(url) 
rep.status_code
200

  

 

 

 

Guess you like

Origin www.cnblogs.com/lcyzblog/p/11258344.html