Python3.x Python requests Module
\nThe Python requests library is a commonly used HTTP request library that allows you to conveniently send HTTP requests to websites and retrieve response results.
\nThe requests module is more concise than the urllib module.
\nTo send HTTP requests using requests, you first need to import the requests module:
\nimport requests\nAfter importing, you can send HTTP requests. Use the methods provided by requests to send HTTP requests to a specified URL, for example:
\nExample
\n# Import requests package\n\nimport requests\n\n# Send request\n\n x = requests.get('')\n\n# Return web page content\n\nprint(x.text)\nEach time a requests call is made, it returns a response object containing specific response information, such as status code, response headers, response content, etc.:
\nprint(response.status_code) # Get response status code\nprint(response.headers) # Get response headers\nprint(response.content) # Get response content\nMore response attributes/methods are listed below:
\n| Attribute or Method | Description |
|---|---|
| apparent_encoding | Encoding method |
| close() | Close connection to server |
| content | Return response content in bytes |
| cookies | Return a CookieJar object containing cookies sent back from the server |
| elapsed | Return a timedelta object representing the time elapsed between sending the request and receiving the response, useful for testing response speed. For example, r.elapsed.microseconds indicates how many microseconds it takes for the response to arrive. |
| encoding | Encoding method for decoding r.text |
| headers | Return response headers in dictionary format |
| history | Return a list of response objects containing the request history (URLs) |
| is_permanent_redirect | Returns True if the response is a permanently redirected URL, otherwise False |
| is_redirect | Returns True if the response was redirected, otherwise False |
| iter_content() | Iterate over response content |
| iter_lines() | Iterate over response lines |
| json() | Return JSON object of the result (requires the result to be in JSON format, otherwise an error will be raised) |
| links | Return parsed header links from the response |
| next | Return the PreparedRequest object for the next request in the redirect chain |
| ok | Checks the value of "status_code". Returns True if less than 400, otherwise False |
| raise_for_status() | If an error occurred, returns an HTTPError object |
| reason | Description of the response status, e.g., "Not Found" or "OK" |
| request | Return the request object that requested this response |
| status_code | Return the HTTP status code, e.g., 404 and 200 (200 means OK, 404 means Not Found) |
| text | Return response content as Unicode string |
| url | Return the URL of the response |
Example
\n# Import requests package\n\nimport requests\n\n# Send request\n\n x = requests.get('')\n\n# Return HTTP status code\n\nprint(x.status_code)\n\n# Description of response status\n\nprint(x.reason)\n\n# Return encoding\n\nprint(x.apparent_encoding)\nOutput result:
\n200 OK utf-8
\nRequesting a JSON data file returns JSON content:
\nExample
\n# Import requests package\n\nimport requests\n\n# Send request\n\n x = requests.get('')\n\n# Return JSON data\n\nprint(x.json())\nOutput result:
\n{'name': 'Website', 'num': 3, 'sites': [{'name': 'Google', 'info': ['Android', 'Google Search', 'Google Translation']}, {'name': '', 'info': ['', 'Runoob Tools', 'Runoob WeChat']}, {'name': 'Taobao', 'info': ['Taobao', 'Online shopping']}]}
\n\n
The requests methods are listed in the table below:
\n| Method | Description |
|---|---|
| delete(_url_, _args_) | Send DELETE request to specified URL |
| get(_url_, _params, args_) | Send GET request to specified URL |
| head(_url_, _args_) | Send HEAD request to specified URL |
| patch(_url_, _data, args_) | Send PATCH request to specified URL |
| post(_url_, _data, json, args_) | Send POST request to specified URL |
| put(_url_, _data, args_) | Send PUT request to specified URL |
| request(_method_, _url_, _args_) | Send specified request method to specified URL |
Sending a GET request using requests.request():
\nExample
\n# Import requests package\n\nimport requests\n\n# Send request\n\n x = requests.request('get','')\n\n# Return web page content\n\nprint(x.status_code)\nOutput result:
\n200
\nSetting request headers:
\nExample
\n# Import requests package\n\nimport requests\n\nkw ={'s':'python Tutorial'}\n\n# Set request headers\n\n headers ={"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36"}\n\n# params Receive a dictionary or string query parameters, dictionary type automatically converts to URL encoding, no need for urlencode()\n\n response = requests.get("", params = kw, headers = headers)\n\n# View response status code\n\nprint(response.status_code)\n\n# View response header character encoding\n\nprint(response.encoding)\n\n# View full URL address\n\nprint(response.url)\n\n# View response content, response.text Data returned in Unicode format\n\nprint(response.text)\nOutput result:
\n200 UTF-8 other content...
\nThe post() method can send a POST request to a specified URL. The general format is as follows:
\nrequests.post(url, data={key: value}, json={key: value}, args)\n- \n
- url The request URL. \n
- data Parameter for a dictionary, list of tuples, bytes, or file object to be sent to the specified URL. \n
- json Parameter for a JSON object to be sent to the specified URL. \n
- args Other parameters, such as cookies, headers, verify, etc. \n
Example
\n# Import requests package\n\nimport requests\n\n# Send request\n\n x = requests.post('')\n\n# Return web page content\n\nprint(x.text)\nOutput result:
\nThis content was requested using the POST method.
Request Time: 2022-05-26 17:30:47
\nPOST request with parameters:
\nExample
\n# Import requests package\n\nimport requests\n\n# Form parameters, parameter names are fname and lname\n\n myobj ={'fname': '','lname': 'Boy'}\n\n# Send request\n\n x = requests.post('', data = myobj)\n\n# Return web page content\n\nprint(x.text)\nOutput result:
\nHello, Boy, how was your day?
\n\n
Additional Request Parameters
\nWhen sending requests, we can attach additional parameters such as headers, query parameters, and request body, for example:
\nheaders = {'User-Agent': 'Mozilla/5.0'} # Set request headers\nparams = {'key1': 'value1', 'key2': 'value2'} # Set query parameters\ndata = {'username': 'example', 'password': '123456'} # Set request body\nresponse = requests.post('', headers=headers, params=params, data=data)\nThe above code sends a POST request with attached headers, query parameters, and request body.
\nIn addition to basic GET and POST requests, requests also supports other HTTP methods such as PUT, DELETE, HEAD, OPTIONS, etc.
YouTip