Question: 4 . Write a class ListParser that is a subclass of the HTMLParser class. It will find and collect the contents of all the list

4. Write a class ListParser that is a subclass of the HTMLParser class. It will find and collect the contents of all the list items, both ordered and unordered, in an HTML file fed into it. The parser works by identifying and remembering, via a class variable, when a list item tag has been encountered. When the data handler for the class is called and the class variable indicates that a list item is currently open, the data in the list item is added to a list in the class. When the list item tag is closed, the class adjusts the internal variable to register this. To implement this parser you will need to override the following methods of the HTMLParser class:
a.__init__: the constructor should call the constructor for the HTMLParser class and create and initialize the necessary class variables
b. handle_starttag: If the tag that resulted in the method being called is a list item, the appropriate class variable should be set.
c. handle_endtag: If the tag that resulted in the method being called is a list item, the appropriate class variable should be unset.
d. handle_data: If the parser is currently inside a list item, the data should be added to the list of list items in the class. Strip any extra spaces or newlines off the contents of the list item before appending it to the class variable.
e. getItems: Returns the list of list items collected by the class.
You can find a template for the class and a test function testLParser() in the assignment zip file. The following shows what the test function would display on some sample web page:1i= csc242hw7.testLParser('
https://www.w3schools.com/html/html_lists.asp')
1i
['Item', 'Item', 'Item', 'Item', 'First item', 'Second item', 'Third item', 'Fourth item']
 4. Write a class ListParser that is a subclass of the

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!