How to Download a Website’s Source Code Using Python

How to Download a Website Source Code Using Python

In this tutorial, we will be looking at how to download a website’s source code using python. We will be implementing the requests library to make HTTP requests to the websites we want to download.

To get started we will need to download requests, open your terminal and run the command shown below.

pip3 install requests

After the module is installed create a python file called main.py and create our program as shown below.

import requests

url = "https://www.bytexplain.com/"
response = requests.get(url)

if response.status_code == 200:
    # The request was successful
    with open("index.html", "wb") as f:
        f.write(response.content)
else:
    # The request was not successful
    print(f"Error: {response.status_code}")

The above code begins by first importing requests and defining the website address to be downloaded. Next, we check if the website is up and running, if so we save its source code to our machine if not we throw an error.

Run the above main.py program to obtain an HTML document containing the source code of the defined domain.

There you have it, Thanks for reading. Happy Coding

Leave a Comment

Your email address will not be published. Required fields are marked *