Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
1d134604
Commit
1d134604
authored
Oct 28, 2011
by
Ezio Melotti
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Improve HTMLParser example in the doc.
parent
5bcd7b10
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
11 additions
and
6 deletions
+11
-6
Doc/library/htmlparser.rst
Doc/library/htmlparser.rst
+11
-6
No files found.
Doc/library/htmlparser.rst
View file @
1d134604
...
...
@@ -186,16 +186,21 @@ An exception is defined as well:
Example HTML Parser Application
-------------------------------
As a basic example, below is a very basic HTML parser that uses the
:class:`HTMLParser` class to print out tags as they are encountered::
As a basic example, below is a simple HTML parser that uses the
:class:`HTMLParser` class to print out start tags, end tags and data
as they are encountered::
from HTMLParser import HTMLParser
class MyHTMLParser(HTMLParser):
def handle_starttag(self, tag, attrs):
print "Encountered the beginning of a %s tag" % tag
print "Encountered a start tag:", tag
def handle_endtag(self, tag):
print "Encountered the end of a %s tag" % tag
print "Encountered an end tag:", tag
def handle_data(self, data):
print "Encountered some data:", data
parser = MyHTMLParser()
parser.feed('
<html><head><title>
Test
</title></head>
'
'
<body><h1>
Parse me!
</h1></body></html>
')
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment