The use of the most detailed and complete requests library in the entire network

1. Introduction

Requests is an HTTP library written in Python, based on Urllib and using the Apache2 Licensed open source protocol. Much more convenient than Urllib.

2. Install Requests

Win+R, enter cmd, open the command line window, and enter the command to install: pip install requests

3. How to use

1. Two important objects of the requests library

r = requests.get(url)

r: is a Response object, an object containing server resources

.get(url): is a Request object that constructs a Request that requests resources from the server.

Get the type of r: type(r)

Show the attributes that r has: dir(r)

The most important and commonly used properties are as follows:

2. Analysis of the main methods of the Requests library

2.1. Introduction of 7 main methods

Remarks: get, head is to obtain information from the server to the local; put, post, patch, delete is to submit information to the server from the local. Resources are managed through urls and commands, operations are independent and stateless, and network channels and servers have become black boxes.

2.2, method writing

r = requests.request(method, url, **kwagrs)

method is the request method, corresponding to 7 methods such as get, head, post, put, patch, delete, options, etc.

url is the url link to get the page

**kwagrs is a parameter to control access, there are 13

注:r = requests.request(method='GET', url = url, **kwargs)等价于r = requests.get(url, **kwargs)

2.3, **kwagrs detailed explanation

**kwagrs is a parameter to control access, all are optional, there are 13 kinds.

(1) params: dictionary or byte sequence, which is added to the url as a parameter. Using this parameter, some key-value pairs can be added to the url in the pattern of ?key1=value1&key2=value2.

(2) data: dictionary, byte sequence or file object, the focus is to provide or submit resources to the server, as the content of requests, unlike params, the data submitted by data is not placed in the url link, but placed in The location corresponding to the url link is stored as data, and it can also accept a string object.

(3) json: data in json format, which is also the most frequently used data format for http, as the content of the request.

(4) headers: dictionary, you can use this field to define the http header of http access, which can be used to simulate any browser we want to simulate to initiate access to the url.

(5) cookies: dictionary or CookieJar, which refers to parsing cookies from http

(6) auth: tuple, used to support http authentication function

(7) files: dictionary, fields used to transfer files to the server

(8) timeout: Set the timeout time in seconds. When a get request is initiated, a timeout time can be set. If the requested content is not returned within the timeout time, a timeout exception will be generated.

(9) proxies: dictionary, used to set access proxy server, can increase login authentication

(10) allow_redirects: switch, indicating whether to allow url redirection, the default is True

(11) stream: switch, refers to whether to download the acquired content immediately, the default is True

(12) verify: switch, used to authenticate SSL integers, the default is True

(13) cert: used to set the path to save the local SSL certificate

Fourth, understand the exceptions of the requests library

Five, understand the exception of Response

If r.status_code is not 200, an exception requests.HTTPError will be generated

The r.raise_for_status() method internally judges whether r.status_code is equal to 200. There is no need to add an additional if statement. This statement is convenient for exception handling with try-except.

Thanks to everyone who read my article carefully, watching the rise and attention of fans all the way, there is always a need for a gift exchange, although it is not a very valuable thing, if you can use it, you can take it directly:

① More than 2000 Python e-books (mainstream and classic books should be available)

② Python standard library information (the most complete Chinese version)

③ Project source code (forty or fifty interesting and classic training projects and source code)

④ Videos on basic introduction to Python, crawler, web development, and big data analysis (suitable for novice learning)


 ⑤ Python learning roadmap (say goodbye to inexperienced learning)

In my QQ technical exchange group (technical exchange and resource sharing, advertisements come in to interrupt you)

You can take it away by yourself. The free information in the group number 913569736 (note "csdn000") is the essence of the author's more than ten years of testing career. There are also peer gods to exchange technology together.

The learning materials can be found by our Miss Beibei [mashan-qq] remarks [csdn000] for free

【Must note】Otherwise it will not pass
 

Guess you like

Origin blog.csdn.net/csdnchengxi/article/details/122619586