summaryrefslogtreecommitdiff
path: root/src/examples/makeHTMLTagExample.py
diff options
context:
space:
mode:
Diffstat (limited to 'src/examples/makeHTMLTagExample.py')
-rw-r--r--src/examples/makeHTMLTagExample.py21
1 files changed, 21 insertions, 0 deletions
diff --git a/src/examples/makeHTMLTagExample.py b/src/examples/makeHTMLTagExample.py
new file mode 100644
index 0000000..e3baf40
--- /dev/null
+++ b/src/examples/makeHTMLTagExample.py
@@ -0,0 +1,21 @@
+import urllib
+
+from pyparsing import makeHTMLTags, SkipTo
+
+# read HTML from a web page
+serverListPage = urllib.urlopen( "http://www.yahoo.com" )
+htmlText = serverListPage.read()
+serverListPage.close()
+
+# using makeHTMLTags to define opening and closing tags
+anchorStart,anchorEnd = makeHTMLTags("a")
+
+# compose an expression for an anchored reference
+anchor = anchorStart + SkipTo(anchorEnd)("body") + anchorEnd
+
+# use scanString to scan through the HTML source, extracting
+# just the anchor tags and their associated body text
+# (note the href attribute of the opening A tag is available
+# as an attribute in the returned parse results)
+for tokens,start,end in anchor.scanString(htmlText):
+ print tokens.body,'->',tokens.href