Last modified: Jul 02, 2023 By Alexander Williams

How to Get Source Code of Website Using Python Requests

You can easily get the source code of a website using Python's requests. To accomplish this goal, you’ll need to end an HTTP GET request to the URL and retrieve the response content.:

Here's an example:

import requests

url = "https://pytutorial.com/"  # Replace with the URL of the website you want to fetch

# Send GET request and retrieve the response
response = requests.get(url)

# Get the source code/content of the website
source_code = response.text

# Print the source code
print(source_code)

In this example, the requests.get() function sends an HTTP GET request to https://pytutorial.com/. 

The .text attribute of the response object returns the HTML source code of the webpage. 

Output:

<!DOCTYPE html><html lang="en" class="no-js"> <head><script async src="https://www.googletagmanager.com/gtag/
  gtag('config', 'UA-154437539-1');
</script><meta name="google-site-verification" content="zIkKeEj6VcYUCtBRzyZBDlIxfq51iYeowr5AK_D9ytM"><meta charset="utf-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"><title>pytutorial | The Simplest Python and Django Tutorial</title><meta name="application-name" content="pytutorial"><meta name="description" content="pytutorial | The Simplest Python and Django Tutorial"><meta name="author" content><link rel="canonical" href="https://pytutorial.com"><meta property="og:url" content="https://pytutorial.com"><meta property="og:type" content="index"><meta property="og:site_name" content="pytutorial"><meta property="og:title" content><meta property="og:description" content="pytutorial"><meta property="og:image" content="https://pytutorial.com/theme/img/logo.png"><meta property="og:image:width" content="1200"><meta property="og:image:height" content="630"><meta name="twitter:card" content="summary_large_image"><meta name="twitter:site" content="@pytutorial"><meta name="twitter:title" content="pytutorial | The Simplest Python and Django Tutorial"><meta name="twitter:image" content="https://pytutorial.com/theme/img/logo.png"><meta name="twitter:description" content="The Simplest Python and Django Tutorial"><meta name="twitter:domain" content="Pytutorial"><meta name="twitter:creator" content="@pytutorial"><meta name="twitter:image:src" content="https://pytutorial.com/theme/img/logo.png"><meta name="thumbnail" content><link href="https://fonts.googleapis.com/css?family=Poppins:400,500,600,700,900" rel="stylesheet"><link href="https://fonts.googleapis.com/css?family=Candal" rel="stylesheet"><link rel="icon" href="/theme/img/logo.png"><link rel="stylesheet" type="text/css" href="/theme/css/comb.css"><link rel="stylesheet" type="text/css" href="/theme/css/style.css"><link href="https://use.fontawesome.com/releases/v5.0.8/css/all.css" rel="stylesheet"><script src="/theme/js/vendor/head_comp.js"></script><!--[if lt IE 9]>
    <script src="js/html5/respond.min.js"></script>
    <![endif]--></head> <body data-spy="scroll" data-target=".navbar" data-offset="60"> <div class="se-pre-con"></div> <header> <nav class="navbar navbar-expand-lg center-brand static-nav"> <div class="container"> <a class="navbar-brand" title="pytutorial-the simplest website tutorial" href="/"> <img width="106" height="106" src="/theme/img/logo.png" alt="pytutorial" class="logo-default"> </a> <div class="d-flex flex-row order-2 order-lg-3"> <ul class="navbar-nav social-item flex-row"> <li class="nav-item"> <a class="nav-link px-3" target="_blank" href="https://www.facebook.com/Pytutorial-108500610683725/?modal=admin_todo_tour"><i class="fab fa-facebook-f"></i></a> </li> <li class="nav-item"> <a class="nav-link px-3" target="_blank" href="https://www.linkedin.com/in/pytutorial-web-865b4519b/"><i class="fab fa-linkedin-in"></i></a> </li> <li class="nav-item"> <a target="_blank" class="nav-link px-3" href="https://twitter.com/pytutorial"><i class="fab fa-twitter"></i></a> </li> <li class="nav-item search-box"> <a class="nav-link px-4" href="#search"><i class="fas fa-search"></i></a> </li> </ul> </div> <button class="navbar-toggler navbar-toggler-right collapsed" type="button" data-toggle="collapse" data-target="#xenav"> <i class="fas fa-bars fa-2x"></i> </button> <div class="collapse navbar-collapse order-3 order-lg-2" id="xenav"> <ul class="navbar-nav ml-auto pr-5"> <li class="nav-item drop_menu"> <a class="nav-link">PYTHON ...