Python: NOTE: The “list item” tag (<li>) has an end item tag (</li>) Write a fun
ID: 3852830 • Letter: P
Question
Python:
NOTE: The “list item” tag (<li>) has an end item tag (</li>)
Write a function liParser(url) which will use a LineItemParser(HTMLParser) class to collect and return a list of all strings which are to be in bold font in a given URL.
Here's what I have:
from html.parser import HTMLParser
from urllib.request import urlopen
import os
class LineItemParser(HTMLParser):
def __init__(self):
self.reset()
self.inListItemTag = False
self.listrings = []
def handle_starting(self, tag, attrs):
if tag == 'li':
self.inListItemTag = True
def handle_tag(self, tag):
if tag == 'li':
self.inListItemTag = False
def handle_data(self, data):
if self.inListItemTag == True:
self.listrings.append(data)
def liParser(url):
l = LineItemParser()
try:
response = urlopen(url)
html = response.read()
except URLError as e:
print(e.reason)
#error
l.feed(html)
print(l.listrings)
there's an error on the l.feed(html)
Help me fix this code!!!!
>>> 1=LineltemPar3er ( ) >>>liParser ('http://facweb.cdm.depaul.edu/jhop1/Level1/page3.html' 'Click here for page 1', 'Click here for page 2', 'Click here for page 5' >>liParser ('http://facweb.cdm.depaul.edu/jhopl/' ['Click here for page,'Click here for page 2*, click here for page 3', Cli ck here for page 4', 'Click here for page 5', "Click here for page x'Explanation / Answer
from html.parser import HTMLParser
from urllib.request import urlopen
import os
class LineItemParser(HTMLParser):
def __init__(self):
self.reset()
self.inListItemTag = False
self.listrings = []
def handle_starting(self, tag, attrs):
if tag == 'li':
self.inListItemTag = True
def handle_tag(self, tag):
if tag == 'li':
self.inListItemTag = False
def handle_data(self, data):
if self.inListItemTag == True:
self.listrings.append(data)
def liParser(url):
l = LineItemParser()
try:
response = urlopen(url)
html = response.read()
except URLError as e:
print(e.reason)
#error
l.feed(html)
print(l.listrings)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.