How to Check Loading Time of Website using Python


We use different websites in our day to day life. Every particular website will take some time to load the content. Mathematically, we can get the loading time by subtracting the time obtained with the reading time of the whole website. In python we have few packages and modules to check loading time of the website.

Steps/Approach

The following are the steps that we need to follow to get the loading time of the website. Let’s see each step one by one.

  • Firstly, we have to import the required libraries into our python environment. The following is the lines of code.

import requests
import time

The requests module is used to get the URLs of the websites and the time module is used to get the time taken by the website to load the content.

  • Now, we will get the start time of the website loading by the time() function of the time module

start_time = time.time()
  • Next, we have to pass the URL that we want to check the loading time to the get() function of the requests module.

response = requests.get("https://www.Tutorialspoint.com")
  • Now again we will get the end time of the website loading by the time() function of the time module.

end_time = time.time()
  • Here in this step we will calculate the loading time of the website by subtracting the start time and end time.

loading_time = end_time - start_time

Example

Let’s combine the above steps of code and get the time taken by the defined website to load the content.

import requests
import time
url = "https://www.Tutorialspoint.com"  
start_time = time.time()
response = requests.get(url)
end_time = time.time()
loading_time = end_time - start_time
print(f"The loading time for the website {url} is {loading_time} seconds.")

Output

The following is the output of the time taken by the website to load the content.

The loading time for the website https://www.Tutorialspoint.com is 0.48038482666015625 seconds.

Example

This is one more example to get the time taken by a website to load the content.

def load_time(url):
    import requests
    import time 
    start_time = time.time()
    response = requests.get(url)
    end_time = time.time()
    loading_time = end_time - start_time
    print(f"The loading time for the website {url} is {loading_time} seconds.")
load_time("https://www.google.com")

Output

The following is the output of the website loading time.

The loading time for the website https://www.google.com is 0.3369772434234619 seconds.

Updated on: 09-Aug-2023

305 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements