Traversing tags
The recursiveChildGenerator method is used to traverse tags, which recursively finds all the tags within tags from the file.
Example:
Python3
# Importing BeautifulSoup class from the bs4 module from bs4 import BeautifulSoup # Opening the html file HTMLFile = open ( "index.html" , "r" ) # Reading the file index = HTMLFile.read() # Creating a BeautifulSoup object and specifying the parser S = BeautifulSoup(index, 'lxml' ) # Using the recursiveChildGenerator method to traverse the html file for TraverseTags in S.recursiveChildGenerator(): # Traversing the names of the tags if TraverseTags.name: # Printing the names of the tags print (TraverseTags.name) |
Output:
How to parse local HTML file in Python?
Prerequisites: Beautifulsoup
Parsing means dividing a file or input into pieces of information/data that can be stored for our personal use in the future. Sometimes, we need data from an existing file stored on our computers, parsing technique can be used in such cases. The parsing includes multiple techniques used to extract data from a file. The following includes Modifying the file, Removing something from the file, Printing data, using the recursive child generator method to traverse data from the file, finding the children of tags, web scraping from a link to extract useful information, etc.