Extracting Web Page Titles from URLs

  • Share this:

Code introduction


This function extracts the title of a web page from a given URL. It uses the requests library to send an HTTP request and BeautifulSoup to parse the HTML content.


Technology Stack : Python, requests, BeautifulSoup

Code Type : Function

Code Difficulty : Intermediate


                
                    
def extract_title_from_url(url, soup):
    # This function extracts the title of a web page from a given URL using BeautifulSoup
    
    # Send a GET request to the URL
    response = requests.get(url)
    
    # Parse the HTML content of the page with BeautifulSoup
    soup = BeautifulSoup(response.text, 'html.parser')
    
    # Find the title tag
    title_tag = soup.find('title')
    
    # Extract and return the title text
    return title_tag.get_text() if title_tag else 'No title found'