Skip to content

Effortlessly Extract Year from Date in Python


Python Tutorials: Extracting the Year from a Date in All H2, H3 Headings

Python is a powerful programming language widely used for various purposes, including data analysis and manipulation. One common task in data analysis is extracting specific information from a date, such as the year. In this tutorial, we will learn how to extract the year from a date in all H2 and H3 headings, using Python.

Before we delve into the code, let’s understand the problem statement. Our objective is to extract the year from a date mentioned in H2 and H3 headings of a web page. We will assume that the web page is in HTML format and contains multiple headings. Our task is to extract the year from each heading that represents a date and store it in a list.

To accomplish this, we will use the BeautifulSoup library, which is a popular Python package for parsing HTML and XML documents. If you haven’t installed it already, you can do so by running the following command:

pip install beautifulsoup4

Once we have BeautifulSoup installed, we can proceed with the code.

Step 1: Import the necessary libraries

First, we need to import the required libraries, namely BeautifulSoup and requests:

from bs4 import BeautifulSoup
import requests

Step 2: Send a request to retrieve the web page content

Next, we will send a GET request to the desired web page and fetch its content:

url = "" # Replace with the actual URL of the web page
response = requests.get(url)
content = response.text

Here, you need to replace the url variable with the actual URL of the web page from which you want to extract the year.

Step 3: Parse the HTML content using BeautifulSoup

Once we have the webpage content, we will parse it using BeautifulSoup:

soup = BeautifulSoup(content, "html.parser")

Step 4: Find all the H2 and H3 headings

Next, we will use BeautifulSoup’s find_all() method to extract all the H2 and H3 headings from the parsed HTML:

headings = soup.find_all(["h2", "h3"])

Step 5: Extract the year from each heading

Now comes the crucial step of extracting the year from each heading. We’ll iterate over the headings and use regular expressions to extract the year from the text:

import re
year_list = []
for heading in headings:
date_pattern = r"\d{4}" # Regular expression pattern to match a 4-digit year
matches = re.findall(date_pattern, heading.text)
if matches:

In the above code snippet, we use the re.findall() method to find all the occurrences of a 4-digit year in each heading. We then append these matches to the year_list if there are any matches found.

Step 6: Print the extracted years

Finally, we can print the extracted years:

for year in year_list:

Alternatively, you can store the extracted years in a file or use them for further analysis as per your requirements.

That’s it! You now know how to extract the year from a date mentioned in H2 and H3 headings using Python. Feel free to customize the code as needed and explore other functionalities of the BeautifulSoup library.

In conclusion, Python provides an efficient way to extract specific information, such as the year from a date, from HTML documents. By combining the power of BeautifulSoup and regular expressions, you can manipulate and extract data from various sources with ease. Happy coding!