This answer key contains 4 multiple-choice, 4 fill-in-the-blank, and 4 output-prediction questions for Python HTML parsing, with the exact answer and a short explanation for every question.
The answers are shown in full, so use this page after making an attempt. The practice page is the better first pass.
Which method handles an opening tag?
Answer: handle_starttag
Why: handle_starttag receives the tag name and its attribute pairs.
What is the attrs argument?
Answer: A list of (name, value) pairs
Why: Convert attrs with dict when you need lookup by attribute name.
Which method receives text between tags?
Answer: handle_data
Why: handle_data is called with text nodes found between tags.
What is the output?
from html.parser import HTMLParser
class P(HTMLParser):
def __init__(self):
super().__init__()
self.tags = []
def handle_starttag(self, tag, attrs):
self.tags.append(tag)
p = P()
p.feed("<h1>A</h1><p>B</p>")
print(p.tags)Answer: ['h1', 'p']
Why: The handler appends each opening tag name in document order.
Complete the HTML parser import.
from html.parser import ____Answer: HTMLParser
Why: HTMLParser is the standard-library base class for the parser.
Complete the base class for a custom parser.
from html.parser import HTMLParser
class P(____):
def handle_starttag(self, tag, attrs):
print(tag)
p = P()
p.feed("<a href='/a'>A</a>")Answer: HTMLParser
Why: A custom parser subclasses HTMLParser.
Complete the call that processes HTML text.
from html.parser import HTMLParser
class P(HTMLParser):
def handle_starttag(self, tag, attrs):
print(tag)
p = P()
p.____("<a href='/a'>A</a>")Answer: feed
Why: feed sends the HTML string through the parser.
Complete the method name for opening tags.
from html.parser import HTMLParser
class P(HTMLParser):
def ____(self, tag, attrs):
print(tag)
p = P()
p.feed("<a href='/a'>A</a>")Answer: handle_starttag
Why: handle_starttag is called for each opening tag.
What does this code print?
from html.parser import HTMLParser
class P(HTMLParser):
def __init__(self):
super().__init__()
self.links = []
def handle_starttag(self, tag, attrs):
values = dict(attrs)
if tag == "a" and values.get("href"):
self.links.append(values["href"])
p = P()
p.feed('<a href="/a">A</a><a href="/b">B</a>')
print(p.links)Answer: ['/a', '/b']
Why: Two anchor tags have href attributes, so both values are collected.
What does the program output?
from html.parser import HTMLParser
class P(HTMLParser):
def __init__(self):
super().__init__()
self.parts = []
def handle_data(self, data):
text = data.strip()
if text:
self.parts.append(text)
p = P()
p.feed("<h1>Title</h1><p>Body</p>")
print(" | ".join(p.parts))Answer: Title | Body
Why: The two non-empty text nodes are joined with a vertical bar.
What appears on the screen?
from html.parser import HTMLParser
class P(HTMLParser):
def __init__(self):
super().__init__()
self.count = 0
def handle_starttag(self, tag, attrs):
if tag == "a":
self.count += 1
p = P()
p.feed('<a href="/a">A</a><a href="/b">B</a>')
print(p.count)Answer: 2
Why: The parser sees two opening anchor tags.
What is printed?
from html import unescape
print(unescape("Tom & Jerry"))Answer: Tom & Jerry
Why: unescape turns the & character reference into an ampersand.
Try the questions first
Open the practice page for Python HTML parsing and answer the same 12 questions with instant feedback.