Finding tags

Tags can be found normally and printed normally using print().

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
Parse = BeautifulSoup(index, 'lxml')
  
# Printing html code of some tags
print(Parse.head)
print(Parse.h1)
print(Parse.h2)
print(Parse.h3)
print(Parse.li)


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.

Similar Reads

Modifying the file

Using the prettify method to modify the HTML code from- https://festive-knuth-1279a2.netlify.app/, look better. Prettify makes the code look in the standard form like the one used in VS Code....

Removing a tag

...

Finding tags

A tag can be removed by using the decompose method and the select_one method with the CSS selectors to select and then remove the second element from the li tag and then using the prettify method to modify the HTML code from the index.html file....

Traversing tags

...

Parsing name and text attributes of tags

Tags can be found normally and printed normally using print()....

Finding Children of a tag

...

Finding all elements of tags

The recursiveChildGenerator method is used to traverse tags, which recursively finds all the tags within tags from the file....