Foreword
Requests: Let HTTP human services, the only Python HTTP library a non-transgenic, human beings can safely enjoy;
Requests inherits all the features urllib2, to meet the current needs of the network, support for Python 2.6-3 + a perfect run. github Open Source Address: https://github.com/kennethreitz/requests
First, the installation environment
1. Using the python mounted pip: pip install request
C:\Users\Administrator>pip install request |
2. Verify request if the installation was successful, cmd input python, and then import request, no error is a success
C:\Users\Administrator>python |
Second, the get request transmission
Xiao Bian using a pycharm, just write the code to improve the efficiency, of course, as long as the python syntax to write what can be, for example, it can be run as a text .py suffix.
1. request directly into the library, get a url to access the small series GET: https://www.cnblogs.com/gsxl/ , then its return information with print.
2.rq response value represents a return after the request, here I direct rq.text, view the contents of the response body.
3. There can view the status codes, response headers, and so on, we introduced later.
import requests |
Third, the get request transmission band parameter params
1. To achieve the Baidu search for "Guangshen dragons," we talk about arguments, there is another no arguments (if in fact this parameter in the url)
2. Enter in the browser: https://www.baidu.com/s?wd= Guangshen dragons , normally open search out, make sure there is no problem after we write the code
3. Define a key-value pair kw (JSON Similarly, where a dictionary), kw = { 'kw': 'GuangShen tiger'}, we only need to add on a get request params = kw to, the following:
Fourth, you can also get more response corresponding value, for example, I want to print a response code, direct print (rq.status_code)
|
Want to know more, we can look at its source code, as well as directly to the following sections: rq.status_code # Response Status Code |
Note that there is a pit: open fiddler when, in pycharm which will send a request error, we fiddler stops (F12) or shut it!