visit
The internet is a treasure trove of information, and sometimes, you might want to access the underlying HTML source code of a website for various purposes like data extraction, analysis, or automation. Python, with its rich ecosystem of libraries, makes web scraping a straightforward task.
In this article, we'll explore how to use Python to fetch the source code of a website.
Before we dive into web scraping, ensure you have Python installed on your system. You can download the latest version from
To install the "Requests" library in Python, you can use the Python package manager pip
. Here are the steps to install Requests:
pip install requests
import requests
# Define the URL of the website you want to scrape
url = '//example.com'
# Send an HTTP GET request to the URL
response = requests.get(url)
# Check if the request was successful (status code 200)
if response.status_code == 200:
# Print the HTML source code
print(response.text)
else:
print('Failed to retrieve the webpage. Status code:', response.status_code)
In this script:
import requests
.url
variable with the URL of the website we want to scrape. You can replace '//example.com'
with the URL of your target website.requests.get(url)
to send an HTTP GET request to the specified URL and store the response in the response
variable.response.text
.
import requests
# Define the URL of the website you want to scrape
url = '//google.com'
# Send an HTTP GET request to the URL
response = requests.get(url)
# Check if the request was successful (status code 200)
if response.status_code == 200:
# Print the HTML source code
print(response.text)
else:
print('Failed to retrieve the webpage. Status code:', response.status_code)
Output:
<!DOCTYPE html>
<html itemscope="" itemtype="//schema.org/WebPage" lang="en">
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<meta content="width=device-width, initial-scale=1.0" name="viewport">
<title>Google</title>
...
</head>
<body>
...
</body>
</html>