Write a program to find the h2 tags from a given html document

In this blog post we are going to learn how to find h2 tags from a html document using BeautifulSoup library

from bs4 import BeautifulSoup
html_doc='''<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>An example HTML page</title>
</head>
<body>
    <h2>H2 Tag</h2>
   <p>ebitis voluptatem aperiam quae pariatur modi ducimus  cupiditate quo veritatis nihil!</p>
   <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Tempora velit voluptas .</p>
</body>
</html>'''
soup = BeautifulSoup(html_doc, 'html.parser')
print("h2 tag of the document:")
print(soup.find("h2"))

Output: 
 
h2 tag of the document: <h2>H2 Tag</h2>

No comments

Powered by Blogger.