Question: processing HTML, re-write default processing action of many tags

  • Thread starter Hubert Hung-Hsien Chang
  • Start date
H

Hubert Hung-Hsien Chang

I know you could use the


def start_a
.....

def end_a
.....

to process the <a href=...> anchor </a> tags, but is there a
default method for processing ALL tags? If I just want change
some parts of the hyperlink and want to keep other parts of the HTML
could I just print them out? There should be such a method.
Can't find it...

Thank you.
 
A

Alex Martelli

Hubert Hung-Hsien Chang said:
I know you could use the


def start_a
....

def end_a
....

to process the <a href=...> anchor </a> tags, but is there a
default method for processing ALL tags? If I just want change
some parts of the hyperlink and want to keep other parts of the HTML
could I just print them out? There should be such a method.
Can't find it...

You could subclass HTMLParser.HTMLParser and override handle_starttag
and handle_endtag (also, if needed, handle_charref, handle_entityref,
and last but not least handle_data -- that's assuming that while you
only talk about processing _tags_ you may in fact also want to process
references and text nodes... possibly handle_comment, too, btw).


Alex
 
M

Michael Foord

I know you could use the


def start_a
....

def end_a
....

to process the <a href=...> anchor </a> tags, but is there a
default method for processing ALL tags? If I just want change
some parts of the hyperlink and want to keep other parts of the HTML
could I just print them out? There should be such a method.
Can't find it...

Thank you.

If you are modifying the contents of tags I've written a simple HTML
parser class called Scraper that does this. Unlike the HTMLParser in
the standard library it doesn't choke so much on badly formed HTML....

It's part of approx.py my cgiproxy....
http://www.voidspace.org.uk/atlantibots/pythonutils.html#cgiproxy

HTH

Regards,

Fuzzy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top