Paul McGuire ptmcg at
Fri Aug 19 20:39:27 CEST 2005

Here's a pyparsing program that reads my personal web page, and spits
out HTML with all of the HREF's reversed.

-- Paul
(Download pyparsing at

from pyparsing import Literal, quotedString
import urllib

LT = Literal("<")
GT = Literal(">")
EQUALS = Literal("=")
htmlAnchor = LT + "A" + "HREF" + EQUALS +
quotedString.setResultsName("href") + GT

def convertHREF(s,l,toks):
    # do HREF conversion here - for demonstration, we will just reverse
    print toks.href
    return "<A HREF=%s>" % toks.href[::-1]

htmlAnchor.setParseAction( convertHREF )

inputURL = ""
inputPage = urllib.urlopen(inputURL)
inputHTML =

print htmlAnchor.transformString( inputHTML )

More information about the Python-list mailing list