Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a class HeaderParser that is a subclass of the HTMLParser class. It will f

ID: 3676759 • Letter: W

Question

Write a class HeaderParser that is a subclass of the HTMLParser class. It will find and collect the contents of all the headings in an HTML file fed to it. The parser works by identifying when a header tag has been encountered and setting a boolean variable in the class to indicate that. When the data handler for the class is called and the boolean in the class indicates that a header is currently open, the data inside the header is added to a list. Finally, when a closing header tag is encountered the boolean variable is unset. To implement this parser you will need to override the following methods of the HTMLParser class:

__init__: The constructor calls the parent class constructor, set the boolean variable in the object appropriately, and sets the list of headings to the empty list.

handle_starttag: If the tag that resulted in this method being called is a header, the header indicator should be set.

handle_endtag: If the tag that resulted in this method being called is a header, the header indicator should be unset.

handle_data: If the parser is currently inside a header, then the data should be added to the list of headers contents. Make sure that you strip any leading or trailing spaces or newlines off the contents of the header before adding it to the list.

getHeadings: The function returns the list of headings gathered by the parser.

Explanation / Answer

(1) Simple Code:

The output will then be like :

(2) Alternate method

Parsing a doctype:

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote