When scraping or analyzing web pages, one of the simplest pieces of metadata to extract is the page title (the text inside the <title> tag).
While the requests library is used for fetching HTML, you'll typically pair it with an HTML parser like BeautifulSoup to extract the title reliably.
Example 1 — Basic Page Title Extraction
This approach uses requests and BeautifulSoup to fetch the page and parse the <title> tag:
import requests
from bs4 import BeautifulSoup
url = "https://example.com"
response = requests.get(url)
soup = BeautifulSoup(response.text, "html.parser")
page_title = soup.title.string if soup.title else "No title found"
print("Title:", page_title)
Good for: Standard static pages with a straightforward <title> tag.
the output is the page title:
Example Domain
Example 2 — Handle Missing or Empty Title
Some pages may not include a <title> tag. You can make your script more robust by checking this:
import requests
from bs4 import BeautifulSoup
url = "https://example.com"
response = requests.get(url)
soup = BeautifulSoup(response.text, "html.parser")
title_tag = soup.find("title")
page_title = title_tag.get_text(strip=True) if title_tag else "Title not found"
print("Page Title:", page_title)
Good for: Pages with inconsistent or malformed HTML.
result is the page title:
Example Domain
Example 3 — Fetch Title with Redirects, Timeout & Errors
For production use, you may want to handle redirects, timeouts, and HTTP errors gracefully:
import requests
from bs4 import BeautifulSoup
def get_title(url):
try:
r = requests.get(url, timeout=(3, 7))
r.raise_for_status()
soup = BeautifulSoup(r.text, "html.parser")
return soup.title.string.strip() if soup.title else "No title"
except requests.exceptions.RequestException as e:
return f"Error: {e}"
print(get_title("https://example.com"))
Good for: Scripts that need error handling and reliability.
result is the page title:
Example Domain
Steps to extract page's title in Python
To extract a page's title in Python:
- Use requests to fetch the HTML.
- Use BeautifulSoup to parse it.
- Check that a
<title>tag exists before reading it.
This is one of the simplest and most common tasks in web scraping and analysis!