.. _urequests: .. module:: urequests :synopsis: HTTP client functions, providing various HTTP request methods :mod:`urequests` --- HTTP client functions --------------------------------------------- Previously we used the socket library, which is a good tool for getting started and helps you understand some basic concepts of web crawlers and master the crawling process. After getting started, we need to learn some more advanced content and tools to make crawling easier. So this section briefly introduces the basic usage of the urequests library. Response Class -------------- .. class:: Response(s) The object of the Response class, which contains the server's response to an HTTP request. - ``s`` - The ussl object Methods ~~~~~~~ .. method:: close() Close the socket. .. decorator:: content Return the content of the response, in bytes. .. decorator:: text Return the content of the response as text, encoded as unicode. .. method:: json() Return the json-encoded content of the response and convert it to a dict type. Methods --------- .. method:: request(method, url, data=None, json=None, headers={},params=None,files=None) Send an HTTP request to the server. - ``method`` - The HTTP method to use - ``url`` - The URL to send to - ``data`` - The body to attach to the request. If a dictionary or a list of tuples is provided, form encoding will be performed. - ``json`` - The json to attach to the body of the request. - ``headers`` - The dictionary of headers to send. - ``params`` - The URL parameters to append to the URL. If a dictionary or a list of tuples is provided, form encoding will be performed. - ``files`` - Used for file upload, of type 2-tuple, which defines the file name, file path, and content type. As follows, {'name', (file directory, content-type)} .. method:: head(url, **kw) Send a HEAD request and return a Response object. - ``url`` - The URL of the Request object - ``**kw`` - The parameters of the request method. .. method:: get(url, **kw) Send a GET request and return a Response object. - ``url`` - The URL of the Request object - ``**kw`` - The parameters of the request method. .. method:: post(url, **kw) Send a POST request and return a Response object. - ``url`` - The URL of the Request object - ``**kw`` - The parameters of the request method. .. method:: put(url, **kw) Send a PUT request and return a Response object. - ``url`` - The URL of the Request object - ``**kw`` - The parameters of the request method. .. method:: patch(url, **kw) Send a PATCH request and return a Response object. - ``url`` - The URL of the Request object - ``**kw`` - The parameters of the request method. .. method:: delete(url, **kw) Send a DELETE request and return a Response object. - ``url`` - The URL of the Request object - ``**kw`` - The parameters of the request method. .. literalinclude:: /_static/products/mpython-s3/_static/examples/network/example_requests.py :caption: requests example :linenos: