YouTip LogoYouTip

Python Requests

Python3.x Python requests Module

\n

The Python requests library is a commonly used HTTP request library that allows you to conveniently send HTTP requests to websites and retrieve response results.

\n

The requests module is more concise than the urllib module.

\n

To send HTTP requests using requests, you first need to import the requests module:

\n
import requests
\n

After importing, you can send HTTP requests. Use the methods provided by requests to send HTTP requests to a specified URL, for example:

\n

Example

\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)
\n

Each time a requests call is made, it returns a response object containing specific response information, such as status code, response headers, response content, etc.:

\n
print(response.status_code) # Get response status code\nprint(response.headers) # Get response headers\nprint(response.content) # Get response content
\n

More response attributes/methods are listed below:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
Attribute or MethodDescription
apparent_encodingEncoding method
close()Close connection to server
contentReturn response content in bytes
cookiesReturn a CookieJar object containing cookies sent back from the server
elapsedReturn 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.
encodingEncoding method for decoding r.text
headersReturn response headers in dictionary format
historyReturn a list of response objects containing the request history (URLs)
is_permanent_redirectReturns True if the response is a permanently redirected URL, otherwise False
is_redirectReturns 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)
linksReturn parsed header links from the response
nextReturn the PreparedRequest object for the next request in the redirect chain
okChecks the value of "status_code". Returns True if less than 400, otherwise False
raise_for_status()If an error occurred, returns an HTTPError object
reasonDescription of the response status, e.g., "Not Found" or "OK"
requestReturn the request object that requested this response
status_codeReturn the HTTP status code, e.g., 404 and 200 (200 means OK, 404 means Not Found)
textReturn response content as Unicode string
urlReturn the URL of the response
\n

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)
\n

Output result:

\n

200 OK utf-8

\n

Requesting a JSON data file returns JSON content:

\n

Example

\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())
\n

Output 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\n\n\n\n\n\n\n\n\n\n\n\n\n
MethodDescription
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
\n

Sending a GET request using requests.request():

\n

Example

\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)
\n

Output result:

\n

200

\n

Setting request headers:

\n

Example

\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)
\n

Output result:

\n

200 UTF-8 other content...

\n

The post() method can send a POST request to a specified URL. The general format is as follows:

\n
requests.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
\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)
\n

Output result:

\n

This content was requested using the POST method.

Request Time: 2022-05-26 17:30:47

\n

POST request with parameters:

\n

Example

\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)
\n

Output result:

\n

Hello, Boy, how was your day?

\n
\n

Additional Request Parameters

\n

When sending requests, we can attach additional parameters such as headers, query parameters, and request body, for example:

\n
headers = {'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)
\n

The above code sends a POST request with attached headers, query parameters, and request body.

\n

In addition to basic GET and POST requests, requests also supports other HTTP methods such as PUT, DELETE, HEAD, OPTIONS, etc.

← Python RandomRef Math Prod β†’