Answer key · 12 questions

Python HTML parsing Exercises With Answers

All 12 questions for Python HTML parsing in one place. Try the practice page first, then use this page to check the exact answer and the reason.

Quick answer

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.

01 · Multiple choice

Which method handles an opening tag?

  1. handle_starttagcorrect
  2. handle_endtag
  3. handle_data
  4. handle_comment

Answer: handle_starttag

Why: handle_starttag receives the tag name and its attribute pairs.

02 · Multiple choice

What is the attrs argument?

  1. A single string
  2. A list of child tags
  3. A dictionary
  4. A list of (name, value) pairscorrect

Answer: A list of (name, value) pairs

Why: Convert attrs with dict when you need lookup by attribute name.

03 · Multiple choice

Which method receives text between tags?

  1. feed
  2. handle_endtag
  3. handle_starttag
  4. handle_datacorrect

Answer: handle_data

Why: handle_data is called with text nodes found between tags.

04 · Multiple choice

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)
  1. []
  2. ['h1', 'p']correct
  3. ['A', 'B']
  4. ['h1']

Answer: ['h1', 'p']

Why: The handler appends each opening tag name in document order.

05 · Fill in the blank

Complete the HTML parser import.

from html.parser import ____

Answer: HTMLParser

Why: HTMLParser is the standard-library base class for the parser.

06 · Fill in the blank

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.

07 · Fill in the blank

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.

08 · Fill in the blank

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.

09 · Predict output

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.

10 · Predict output

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.

11 · Predict output

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.

12 · Predict output

What is printed?

from html import unescape
print(unescape("Tom &amp; Jerry"))

Answer: Tom & Jerry

Why: unescape turns the &amp; 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.

Practice this lesson