Extract Random Data from HTML Tags

  • Share this:

Code introduction


This function extracts random data from the given HTML content based on a specified tag name. If multiple tags are found, it randomly selects one and returns its text content.


Technology Stack : beautifulsoup4

Code Type : Function

Code Difficulty : Intermediate


                
                    
import random
from bs4 import BeautifulSoup, SoupStrainer

def extract_random_data(html_content, tag_name):
    """
    Extracts random data from a given HTML content based on a specified tag name.
    
    Args:
    html_content (str): The HTML content to parse.
    tag_name (str): The tag name to extract data from.
    
    Returns:
    str: The extracted data or None if no data is found.
    """
    # Initialize BeautifulSoup with a SoupStrainer to parse only the specified tag
    soup = BeautifulSoup(html_content, 'html.parser', parse_only=SoupStrainer(tag_name))
    
    # Find all instances of the specified tag
    tags = soup.find_all(tag_name)
    
    # If no tags are found, return None
    if not tags:
        return None
    
    # Randomly select a tag and return its text
    return random.choice(tags).get_text()

# Code Information