[Reptile] two, request and response

Request Libraries

本节目标:掌握定向网页爬取和解析的基本能力

Simple summary Request library role: the site is seen as an object, a request to send the request, the request can be modified by a number of parameters (such as custom head), returns a response object, then you can get access to the site by way of property header information, the encoding format, web content, etc., response is the basics of subsequent pages resolved.

First, the main method -get Requests Library ():

1, format

Here Insert Picture Description
Here Insert Picture Description

2, Response object's properties:

Here Insert Picture Description
Wherein the text string is returned by the format, content is returned byte stream.
Supplementary: r.headers return header information.
About encoding → r.encoding: if there is no char-set header, it is considered as the encoding ISO-8859-1, r.text r.encoding web content according to the display. r.apparent_encoding: analysis of the coding based on page content.

3, exception handling

Response.raise_for_status( )

4, generic code frame

Here Insert Picture Description
Introduction of another library method request
Here Insert Picture Description
Patch partial update explained: Location URL Suppose a set of data UserInfo, including UserID, UserName field 20 and other requirements: the user to modify the UserName, the other unchanged. The use PATCH, submitted UserName local update request using PUT only to URL, all 20 fields must be submitted to the URL, uncommitted field is removed. So, the most important benefit PATCH is to save network bandwidth.

Two, request.post method

Post a dictionary, the form is automatically encoded into a form. Attribute data string encoded as:
payload = {K1: "V1", K2: "V2"}
R & lt request.POST = (URL = 'HTTP: //httpbin.org', = payload data)
the Print (r.text ( ))
Here Insert Picture Description

Third, access control parameters

params: byte sequence or the dictionary (the string code is a sequence of bytes, the type of bytes), is added as a parameter to the url.
3.1 cert: local SSL certificate path.
3.2 data: dictionary, a sequence of bytes or a file object, a method generally used in combination with the post, as a data push, if the configuration of the dictionary form field, if the response is str constituting the data field.
3.3 json: json format data, and the data are similar as the response of a domain.
3.4 headers: dictionary, used to customize visit http head, for example, set the proxy name.
3.5 Cookies:. dictionary format
3.6 auth: tuples, authentication.
3.7 files: a dictionary file transfer.
3.8 timeout: set the timeout time, in seconds.
3.9 Proxies: a dictionary, setting access proxy.
3.10 allow_redirects: True / False, the default is True, the redirection switching.
3.11 Stream : True / False, the default is True, acquiring content for immediate download switch.
3.12 the Verify: True / False, the default is True, SSL certificate authentication switch.
test code: Request library learn

Published 12 original articles · won praise 1 · views 266

Guess you like

Origin blog.csdn.net/weixin_43522964/article/details/104090342