Get a list of all the heading tags using BeautifulSoup
In order to print all the heading tags using BeautifulSoup, we use the find_all() method. The find_all method is one of the most common methods in BeautifulSoup. It looks through a tag and retrieves all the occurrences of that tag.
Syntax: find_all(name, attrs, recursive, string, limit, **kwargs)
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
An HTML document consists of the following tags – h1, h2, h3, h4, h5, and h6. The most commonly used HTML tags in webpages are h1, h2 and h3, and to find these we pass a list of tags as an argument to the find_all() method.
- import the libraries requests and BeautifulSoup
- pass a URL into a variable
- use the request library to fetch the URL
- create a BeautifulSoup object
- create a list of heading tags ()
- iterate over all the heading tags using find_all() method
h2 -> Related Articles h2 -> Python3 h2 -> Python3 h2 -> Python3 h2 -> Python3 h2 -> Python3 h2 -> Python3