Last active
February 26, 2023 21:06
-
-
Save eder-projetos-dev/b72c37a89e6004aa351431a04d121a7f to your computer and use it in GitHub Desktop.
Python - What is the HTML parser?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from html.parser import HTMLParser | |
class Parser(HTMLParser): | |
# method to append the start tag to the list start_tags. | |
def handle_starttag(self, tag, attrs): | |
global start_tags | |
start_tags.append(tag) | |
# method to append the end tag to the list end_tags. | |
def handle_endtag(self, tag): | |
global end_tags | |
end_tags.append(tag) | |
# method to append the data between the tags to the list all_data. | |
def handle_data(self, data): | |
global all_data | |
all_data.append(data) | |
# method to append the comment to the list comments. | |
def handle_comment(self, data): | |
global comments | |
comments.append(data) | |
start_tags = [] | |
end_tags = [] | |
all_data = [] | |
comments = [] | |
# Creating an instance of our class. | |
parser = Parser() | |
# Poviding the input. | |
parser.feed('<html><title>Desserts</title><body><p>' | |
'I am a fan of frozen yoghurt.</p><' | |
'/body><!--My first webpage--></html>') | |
print("start tags:", start_tags) | |
print("end tags:", end_tags) | |
print("data:", all_data) | |
print("comments", comments) |
What is the HTML parser in Python?
Educative Answers Team
https://www.educative.io/answers/what-is-the-html-parser-in-python
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
python3 html_parser_exemplo.py
start tags: ['html', 'title', 'body', 'p']
end tags: ['title', 'p', 'body', 'html']
data: ['Desserts', 'I am a fan of frozen yoghurt.']
comments ['My first webpage']