Scrap a webpage site title using Python

Scrap a webpage site title using Python

 

Description:

To scrap a webpage title first we need to have some libraries installed.
Libraries like ‘request’ and ‘BeautifulSoup’ can be used to scrap the title.

Implementation:

import requests
from bs4 import BeautifukSoup

def get_webpage_title(url):
try:
response = requests.get(url)
response.raise_for_status()

soup = BeautifulSoup(response.content, ‘html.parser’)
title = soup,title.string if soup.title else “NO title found”
return title
except request.exceptions.RequestException as e:
return f”An error occured: {e}”

url = “https://amazon.com”

title = get_webpage_title(url)

print(“Title:”, title)

Output:

Title :  Welcome to  amazon.com

Leave a Comment

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

Scroll to Top