[Zope] Accessing DOM nodes from python: how?

Passin, Tom tpassin@mitretek.org
Fri, 20 Dec 2002 16:40:06 -0500


[ Catonano]

> I saw a javascript accessing a value put in a html tag like this:
>=20
>=20
> <TD ID=3D "blah">
> Td contents
> </TD>
>=20
> or something similar.
>=20
> Javavascript could read that "blah"
>=20
> Now, how can I do the same from python? Can I?
>=20

I am not at all sure what you want to do.  When the javascript script
accesses that value, it is operating in the browser.  Python will be in
the server.  Why should it access the page at all?  If there is data to
be sent to Python, just send it the data.  The page, in fact, will not
actually exist for Python to "access".  Only after your python scripts
are done will the page be fully assembled.

If you mean that you want to write some python code that you feed an
html page to, that is not how you would normally use Zope. =20

If you want Python to access a web page and retrieve some data from it
while it is processing a REQUEST, that is best done using an external
method.  There are a variety of ways to process a web page, but it is
not always easy, given the amazing range of invalid html code that
exists out there.  Sometimes it is easiest to use regular expressions,
and sometimes it is easiest to use the sgmlop parser (you have to write
a handler, but that is not too hard depending on what you need to do). =20

Or you can turn the html page into xml using HTML Tidy, then use xml
processing on the page.  If you do this and use the PyXML beta (or maybe
it is still alpha) package, you can get access to the W3C DOM API, and
access the page element content that way.

Cheers,

Tom P