This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Different behavior of html.parser.HTMLParser
Type: behavior Stage: resolved
Components: Documentation Versions: Python 3.2
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: ezio.melotti Nosy List: docs@python, ezio.melotti, fdrake, hansokumake
Priority: normal Keywords:

Created on 2012-06-21 10:00 by hansokumake, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (3)
msg163318 - (view) Author: hansokumake (hansokumake) Date: 2012-06-21 10:00
I tried this example from the documentation:

from html.parser import HTMLParser

class MyHTMLParser(HTMLParser):
    def handle_starttag(self, tag, attrs):
        print("Encountered a start tag:", tag)
    def handle_endtag(self, tag):
        print("Encountered an end tag :", tag)
    def handle_data(self, data):
        print("Encountered some data  :", data)

parser = MyHTMLParser(strict=False)
parser.feed('<html><head><title>Test</title></head>'
            '<body><h1>Parse me!</h1></body></html>')



According to documentation the output should be like this:
Encountered a start tag: html
Encountered a start tag: head
Encountered a start tag: title
Encountered some data  : Test
Encountered an end tag : title
Encountered an end tag : head
Encountered a start tag: body
Encountered a start tag: h1
Encountered some data  : Parse me!
Encountered an end tag : h1
Encountered an end tag : body
Encountered an end tag : html

but Python produced this:
Encountered some data  : <html>
Encountered some data  : <head>
Encountered some data  : <title>
Encountered some data  : Test
Encountered an end tag : title
Encountered an end tag : head
Encountered some data  : <body>
Encountered some data  : <h1>
Encountered some data  : Parse me!
Encountered an end tag : h1
Encountered an end tag : body
Encountered an end tag : html


If strict is set to True, it works correctly.
msg163331 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2012-06-21 13:41
What exact version of python have you used?
The example works here with 3.2.3+.
msg163345 - (view) Author: hansokumake (hansokumake) Date: 2012-06-21 16:27
I'm sorry. It's my fault. I still use Python 3.2.2.
History
Date User Action Args
2022-04-11 14:57:31adminsetgithub: 59325
2012-06-21 16:35:13ezio.melottisetresolution: not a bug
stage: resolved
2012-06-21 16:27:53hansokumakesetstatus: open -> closed

messages: + msg163345
2012-06-21 13:41:21ezio.melottisetassignee: docs@python -> ezio.melotti
messages: + msg163331
2012-06-21 10:33:20fdrakesetnosy: + fdrake
2012-06-21 10:24:00r.david.murraysetnosy: + ezio.melotti
2012-06-21 10:00:39hansokumakecreate