Home > other >  The use of python - request module
The use of python - request module

Time:09-16

A, install
1. The PIP install
Under the Windows system only need to command at the command line input PIP install requests to install
Under Linux system, only need to type the command sudo PIP install requests, can install,
2. The installation package to install
Download address: https://github.com/requests/requests
Run the command line and type: python setup. Py install
3. Installation pychrom
File - & gt; Settings - & gt; Project: python - & gt; Project Interpreter - & gt; Install - & gt; In the search box to search requests - & gt;
The Install Package
Specify the version specified version
The Options to specify the installation path
4. The import of the project: import requests
The use of the two, requests module
Common way of request
1.(1). The GET: request to the specified page information, and returns the entity body,
(2). The HEAD: only the requested page first,
(3). POST: request to the server to accept as specified by the document as the URI of the new logo by subordinate entities,
(4). The PUT: from the client to the server transmits the data to replace the specified content of the document,
(5). The DELETE: request to the server to DELETE the specified page,
Note: the get and post are common get request will be submitted data is placed in the HTTP request protocol header, post submitted data is placed in the entity data
2. Requests the library methods of seven major
Methods to explain
Requests. The request () constructs a request, supports the following methods
Requests. The get () to obtain the main method of HTML
Requests. The head () the major methods of HTML header information for
Requests. Post () to the HTML web page to submit the post request method
Requests. The put () method of submit a put request to HTML web page
Requests. Patch () local modification request submit to the HTML
Requests. The delete () to submit a removal request to HTML
3. Requests. The get () parsing
 r=requests. Get (url, params, * * kwargs) 

Url: need to crawl the web address
Example:
 r=requests. Get (' http://httpbin.org/get ') 

Results:
{
"Args" : {},
"Headers" : {
"Accept" : "*/*",
"Accept - the Encoding", "gzip, deflate",
"The Host" : "httpbin.org",
"The user-agent" : "python - requests/2.23.0,"
"X - Amzn - Trace - Id" : "Root=1-5 ed75df5-526882 d5f959312833b6b506"
},
"Origin", "125.121.233.25",
"Url" : "http://httpbin.org/get"
}


Params: the parameter is the translation, additional configuration parameters in the url, dictionary or byte stream format, optional
Example:
 params={' name ':' Zy '} 
R=requests. Get (' http://httpbin.org/get ', params=params)
Print (r.t ext)

Results:
{
"Args" : {
"Name" : "Zy"
},
"Headers" : {
"Accept" : "*/*",
"Accept - the Encoding", "gzip, deflate",
"The Host" : "httpbin.org",
"The user-agent" : "python - requests/2.23.0,"
"X - Amzn - Trace - Id" : "Root=1-5 ed75d3e - 84578248 cd064904f5c1501c"
},
"Origin", "125.121.233.25",
"Url" : "http://httpbin.org/get? Name=Zy
"}
* * kwargs: the parameters of the access control of a 12
The parameters of the * * kwargs details
(1). Params: a dictionary or a sequence of bytes, to increase the url as a parameter, use this parameter to some key/value pair to? Key1=value1 & amp; Key2=value2 model added to the url
(2). Data: a dictionary, byte sequence or the file object, key as submit to the server or resource is submitted, and as the content of the request, unlike params, data submitted data is not in the url, but in the url links corresponding location as the data to store, and it can also accept a string object,
Example: in the post request, the general data will be in the request message, not directly shown in the URL, could lead to a post request demo
World
 data='https://bbs.csdn.net/topics/Hello' 
R=requests. Post (' http://httpbin.org/post 'data=https://bbs.csdn.net/topics/data)
Print (r.t ext)

Results:
{
"Args" : {},
"Data" : "Hello World",
"Files" : {},
"The form" : {},
"Headers" : {
"Accept" : "*/*",
"Accept - the Encoding", "gzip, deflate",
"The Content - Length", "11",
"The Host" : "httpbin.org",
"The user-agent" : "python - requests/2.23.0,"
"X - Amzn - Trace - Id" : "Root=1-5 ed760ca - 9 f048ddcee9f29143586b9d0"
},
"Json" : null,
"Origin", "125.121.233.25",
"Url" : "http://httpbin.org/post"
}
(3). The json: json data format, json right in the related HTML, HTTP related web development is very common, is also the most frequently used HTTP data format, he is as a content part can be submitted to the server,
Example:
 josn={
"Pages" : [
"Pages/index/index",
"Pages/logs/logs"
],
"The window" : {
"BackgroundTextStyle", "light",
"NavigationBarBackgroundColor" : "# FFF,"
"NavigationBarTitleText" : "WeChat",
"NavigationBarTextStyle" : "black"
}
}

R=requests. Post (' http://httpbin.org/post ', json=josn)
Print (r.t ext)

Results:
{
"Args" : {},
"Data" : "{" pages \ ": [" pages/index/index \ ", \ "pages/logs/logs \], " window \ ": {" backgroundTextStyle \ ": " light \ ", \ "navigationBarBackgroundColor " : \ "# FFF ", \ "navigationBarTitleText " : \ "WeChat ", \ "navigationBarTextStyle " : \ "black "}} ",
"Files" : {},
"The form" : {},
"Headers" : {
"Accept" : "*/*",
"Accept - the Encoding", "gzip, deflate",
"The Content - Length", "206",
"The content-type" : "application/json,"
"The Host" : "httpbin.org",
"The user-agent" : "python - requests/2.23.0,"
"X - Amzn - Trace - Id" : "Root=1-5 ed774f6-592 ec426b9aea6b0c689ca7e"
},
"Json" : {
"Pages" : [
"Pages/index/index",
"Pages/logs/logs"
],
"The window" : {
"BackgroundTextStyle", "light",
"NavigationBarBackgroundColor" : "# FFF,"
"NavigationBarTextStyle" : "black",
"NavigationBarTitleText" : "WeChat"
}
},
"Origin", "125.121.233.25",
"Url" : "http://httpbin.org/post"
}
(4). Headers: dictionary is HTTP related to language, the corresponding to a url to access the HTTP header fields, led by the HTTP access can use this field to define the HTTP headers, which can be used to simulate any we want to simulate a browser to the url access,
Note: generally is used to avoid website crawler, headers contain some browser information, can be simply by adding headers bypass
 headers={' the user-agent ':' Mozilla/5.0 (Windows NT 10.0; Win64. X64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36 '} 
R=requests. Post (' http://httpbin.org/post 'headers=headers)
Print (r.t ext)

Results:
{
"Args" : {},
"Data" : ", "
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related