Trying to understand html.parser.HTMLParser

Karim karim.liateni at free.fr
Tue May 17 16:26:13 EDT 2011


On 05/17/2011 03:05 AM, Andrew Berg wrote:
> On 2011.05.16 02:26 AM, Karim wrote:
>> Use regular expression for bad HTLM or beautifulSoup (google it), below
>> a exemple to extract all html links:
>>
>> linksList = re.findall('<a href=(.*?)>.*?</a>',htmlSource)
>> for link in linksList:
>>       print link
> I was afraid I might have to use regexes (mostly because I could never
> understand them).
> Even the BeautifulSoup website itself admits it's awful with Python 3 -
> only the admittedly broken 3.1.0 will work with Python 3 at all.
> ElementTree doesn't seem to have been updated in a long time, so I'll
> assume it won't work with Python 3.
> lxml looks promising, but it doesn't say anywhere whether it'll work on
> Python 3 or not, which is puzzling since the latest release was only a
> couple months ago.
>
> Actually, if I'm going to use regex, I might as well try to implement
> Versions* in Python.
>
> Thanks for the answers!
>
> *http://en.totalcmd.pl/download/wfx/net/Versions (original, made for
> Total Commander) and
> https://addons.mozilla.org/en-US/firefox/addon/versions-wfx_versions/
> (clone implemented as a Firefox add-on; it's so wonderful, I even wrote
> the docs for it!)

Andrew,

I wrote a class with HMLTParser to get only one link for a given 
project, cf below:

   73 class ResultsLinkParser(HTMLParser.HTMLParser):
   74     """Class ResultsLinkParser inherits form HTMLParser to extract
   75     the original 'Submission date' of the a bug.
   76     This customized parser will deals with the 'View Defect' HTML
   77     page from Clear DDTS.
   78     """
   79     def __init__(self):
   80         HTMLParser.HTMLParser.__init__(self)
   81         self._link = None
   82
   83     def handle_starttag(self, tag, attrs):
   84         """Implement standard class HTMLParser customizing method."""
   85         if tag == 'frame':
   86             try:
   87                 attributes = dict(attrs)
   88                 if attributes['name'] == 'indexframe':
   89                     self._link = attributes['src']
   90             except KeyError, e:
   91                 print("""WARNING: Attribute '{keyname}' from frame tag
   92                   in QueryResult page does not 
exist!""".format(keyname=e))
   93
   94     def link(self):
   95         """Return the html link of the query results page."""
   96         return self._link

You can use it and just modified it to get the latest just add some code 
(and change the tag 'name' of my example) to compare revision number 
with max and keep the max to compare it to the next value. I let you add 
this little code just create self._revision = None in the __init__(self) 
which hold the current max revision. After parser.feed() you can get the 
value by parser._revision or a public parser.revision() method to get 
the value.

Cheers
Karim





More information about the Python-list mailing list