The Response object

The Response object contains all the information returned by the server, as well as the request information requested.

R = requests. Get (URL) | | Response Request The reponse object contains the content returned by the crawlerCopy the code

Property of the Response object

attribute instructions
r.status_code Return status of the HTTP request. 200 indicates a successful connection and 404 indicates a failed connection
r.text The HTTP response content is a string, that is, the page content corresponding to the URL
r.encoding Guess the encoding of the response content from the HTTP header
r.apparent_encoding Encoding method of response content analyzed from content (alternative encoding method)
r.content The binary form of the HTTP response content
  • R.e ncoding: If no charset exists in the header, the coding bit ISO-8859-1 is considered. R.ext displays web page content according to R.encoding.
  • R.aparent_encoding: Encoding based on the content of the web page, which can be considered as an alternative to R.encoding.

The Requests library exception

abnormal instructions
requests.ConnectionError The network connection is incorrect, for example, DNS query failure or connection denial
requests.HTTPError HTTP error exception
requests.URLRequired URL missing exception
requests.TooManyRedirects The maximum number of redirection times is exceeded, causing a redirection exception. Procedure
requests.ConnectTimeout The connection to the remote server timed out. Procedure
requests.Timeout The REQUEST URL timed out. Procedure
r.raise_for_status If it is not 200, an exception requests.HTTPError is raised
  • R.aise_for_status () determines whether r.tatus_code is equal to 200 within the method, without the need for additional if statements, which facilitate try-except exception handling.
try:
	r = requests.get("http://www.baidu.com")
	r.raise_for_status()
	print(r.text)
except:
	print('error')
Copy the code

The main method of the Requests library

Requests the library methods HTTP protocol methods instructions
requests.request() / Request Constructs a request that supports the underlying methods for each of the following methods
requests.get() GET Request a resource at the URL location
requests.head() HEAD Request a response message report for a URL location resource, that is, get the header information for that resource
requests.post() POST Append new data to the resource at the URL location
requests.put() PUT Request to store a resource at the URL location, overwriting the resource at the original URL location
requests.patch() PATCH Requests a local update of a resource at a URL location, that is, changes part of the content of that resource
requests.delete() DELETE Request to delete the resource stored at the URL location
Difference between PATCH and PUT

Assume that the URL location has a set of data, UserInfo, including UserID, UserName, and other 20 fields. Requirement: The user has changed the UserName

  • PATCH is used to submit only the local update request of UserName to the URL
  • With PUT, all 20 fields must be submitted to the URL together, and unsubmitted fields are deleted

The main benefit of PATCH is to save network bandwidth

Main method parameter description

Requests. Request (method, URL, **kwargs)

  • Method: indicates the request mode, including get, POST, and PUT
  • Url: The URL link of the page to be obtained
  • **kwargs: 13 optional parameters to control access

To access parameters

  • Params: Parameters, dictionaries, or byte sequences that can be added to a URL
import requests kv = {'key1': 'value1', 'key2': 'value2'} r = requests. Request ('GET', 'http://python123.io/ws', params = kv) print(r.uri) output: https://python123.io/ws? key1=value1&key2=value2Copy the code
  • Data: dictionary, byte sequence, or file object. The focus is on providing or submitting resources to the server.
  • Json: Data in JSON format, used as the content of the Request
  • Headers: dictionary that can be used to customize the HTTP protocol headers for accessing a URL.
  • Cookies: dictionary or CookieJar, cookies in Request
  • Auth: a tuple that supports HTTP authentication
  • Files: Dictionary type, used for transferring files to the server.
fs = {'file': open('data.txt', 'rb')}
r = requests.request('POST', 'http://python123.io/ws', files = fs)
Copy the code
  • Timeout: Sets the timeout time, in seconds. If our request is not returned, it will generate a timeout exception.
  • Proxies: Dictionary type, set access to proxy servers, can add login authentication
  • Allow_redirects: True/False, default is True, redirect switch
  • Stream: True/False, default is True, get content immediately download switch
  • Verify: True/False. The default value is True. Authenticates SSL certificates
  • Cert: indicates the path of the local SSL certificate