HyperText Markup Language support in Python?

PythonServer Side ProgrammingProgramming

Python has the capability to process the HTML files through the HTMLParser class in the html.parser module. It can detect the nature of the HTML tags their position and many other properties of the tags. It has functions which can also identify and fetch the data present in an HTML file.

In the below example we see how to use the HTMLParser class to create a custom parser class which can only process the tags and data that we define in the class. Here we are processing the start tag, end tag and data.

Below is the html which is getting processed by the python custom parser.

Example

<html>
<br>
<head>
<br>
<title>welcome to Tutorials Point!</title>
<br>
</head>
<br>
<body>
<br>
<h1>Learn anything !</h1>
<br>
</body>
<br>
</html>

Below is the program which parser the above file and then outputs the result as per a custom parser.

Example

from html.parser import HTMLParser
import io
class Custom_Parser(HTMLParser):
   def handle_starttag(self, tag, attrs):
      print("Line and Offset ==", HTMLParser.getpos(self))
      print("Encountered a start tag:", tag)


   def handle_endtag(self, tag):
      print("Line and Offset ==", HTMLParser.getpos(self))
      print("Encountered an end tag :", tag)


   def handle_data(self, data):
      print("Line and Offset ==", HTMLParser.getpos(self))
      print("Encountered some data :", data)

parser = Custom_Parser()

stream = io.open("E:\\test.html", "r")
parser.feed(stream.read())

Output

Running the above code gives us the following result −

Line and Offset == (1, 0)
Encountered a start tag: html
Line and Offset == (1, 6)
Encountered some data :

Line and Offset == (2, 0)
Encountered a start tag: head
Line and Offset == (2, 6)
Encountered some data :

Line and Offset == (3, 0)
Encountered a start tag: title
Line and Offset == (3, 7)
Encountered some data : welcome to Tutorials Point!
Line and Offset == (3, 34)
Encountered an end tag : title
Line and Offset == (3, 42)
Encountered some data :

Line and Offset == (4, 0)
Encountered an end tag : head
Line and Offset == (4, 7)
Encountered some data :

Line and Offset == (5, 0)
Encountered a start tag: body
Line and Offset == (5, 6)
Encountered some data :

Line and Offset == (6, 0)
Encountered a start tag: h1
Line and Offset == (6, 4)
Encountered some data : Learn anything !
Line and Offset == (6, 20)
Encountered an end tag : h1
Line and Offset == (6, 25)
Encountered some data :

Line and Offset == (7, 0)
Encountered an end tag : body
Line and Offset == (7, 7)
Encountered some data :

Line and Offset == (8, 0)
Encountered an end tag : html
raja
Published on 12-Jan-2021 13:07:52
Advertisements