Question: Write a custom parser named ListParser. It will find any list element and print of the values to the screen. Note. You should remove any

Write a custom parser named ListParser. It will find any list element and print of the values to the screen. Note. You should remove any blank list items.

Write a custom parser named ListParser. It will find any list element

from html.parser import HTMLParser

from urllib.parse import urljoin

from urllib.request import urlopen

def testParser(url):

content = urlopen(url).read().decode()

parser = ListParser()

parser.feed(content)

return parser.getData()

class ListParser(HTMLParser):

def __init__(self):

pass

def handle_starttag(self, tag, attrs):

pass

def handle_data(self, data):

pass

def getData(self):

pass

def testHParser(url):

content = urlopen(url).read().decode()

parser = HeaderParser()

parser.feed(content)

return parser.getHeadings()

import re

from html.parser import HTMLParser

class HeaderParser(HTMLParser):

def __init__(self):

super().__init__()

self.in_header = False

self.headers_list = []

def handle_starttag(self, tag, attrs):

if re.match(r'h[1-6]', tag):

self.in_header = True

def handle_endtag(self, tag):

if re.match(r'h[1-6]', tag):

self.in_header = False

def handle_data(self, data):

if self.in_header:

self.headers_list.append(str(data).strip())

def getHeadings(self):

return self.headers_list

>>> testParser ('http://zoko.cam.depaul.edu/csc242/lists.html' 'Item 1', 'This is a nested 1ist','This is a second item in the nested 1ist', Item 2' Third item', Item A',Item B

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!