Skip to content
Related Articles

Related Articles

Improve Article

Find the title tags from a given html document using BeautifulSoup in Python

  • Last Updated : 15 Mar, 2021

Let’s see how to Find the title tags from a given html document using BeautifulSoup in python. so we can find the title tag from html document using BeautifulSoup find() method. The find function takes the name of the tag as string input and returns the first found match of the particular tag from the webpage.

Example 1:

Python




# import BeautifulSoup
from bs4 import BeautifulSoup
  
# create html document
html = """
      <html>
            <head>
                  <title>   GREEKSFORGREEKS   </title>
            </head>
      <body>
               
<p>     GFG BeautifulSoup tutorial
             </p>
  
      </body>
     </html>
"""
  
# invoke BeautifulSoup()
soup = BeautifulSoup(html, 'html.parser')
print("  ***  Title of the document  ***  ")
  
# invoke find()
print(soup.find("title"))

Output:



Example 2:

Python




# import BeautifulSoup
from bs4 import BeautifulSoup
  
# create html document
html = """
      <html>
            <head>
                  <title>   Hi I am Title    </title>
            </head>
      <body>
               
<p>     GFG BeautifulSoup tutorial
             </p>
  
      </body>
     </html>
"""
  
# invoke BeautifulSoup()
soup = BeautifulSoup(html, 'html.parser')
print("  ***  Title of the document  ***  ")
  
# invoke find()
print(soup.find("title"))

Output:

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course




My Personal Notes arrow_drop_up
Recommended Articles
Page :