Interface Automation python transmits a get request 2-

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
Collecting request
Using cached https://files.pythonhosted.org/packages/f1/27/7cbde262d854aedf217061a97020d66a63163c5c04e0ec02ff98c5d8f44e/request-2019.4.13.tar.gz
Requirement already satisfied: get in d:\path_python\lib\site-packages (from request) (2019.4.13)
Requirement already satisfied: post in d:\path_python\lib\site-packages (from request) (2019.4.13)
Requirement already satisfied: setuptools in d:\path_python\lib\site-packages (from request) (39.0.1)
Requirement already satisfied: query_string in d:\path_python\lib\site-packages (from get->request) (2019.4.13)
Requirement already satisfied: public in d:\path_python\lib\site-packages (from query_string->get->request) (2019.4.13)
Installing collected packages: request
Running setup.py install for request ... done
Successfully installed request-2019.4.13
WARNING: You are using pip version 19.2.3, however version 19.3.1 is available.
You should consider upgrading via the 'python -m pip install --upgrade pip' command.

2. Verify request if the installation was successful, cmd input python, and then import request, no error is a success

C:\Users\Administrator>python
Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 17:00:18) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import request
>>>

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

url = 'https://www.cnblogs.com/gsxl/'
rq = requests.get(url)
print(rq.text)


 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)


Requests Import

kW = { 'WD': 'GuangShen tiger'}
RQ = requests.get ( "? http://www.baidu.com/s", the params = kW)

print (rq.status_code) status code #
print (rq.text) # raw response text
print (rq.headers) # response headers and so on ...

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
rq.content # automatically decode
rq.headers # response header
rq.json () # json decoder
rq.url # acquires URL
rq.encoding # encoding format, generally set = 'UTF-. 8'
RQ .cookies # get the cookie
rq.raw # original response body
rq.text # response body

 Note that there is a pit: open fiddler when, in pycharm which will send a request error, we fiddler stops (F12) or shut it! 

Guess you like

Origin www.cnblogs.com/gsxl/p/11709197.html