executing JavaScript from Python

Geoff Talvola gtalvola at nameconnector.com
Thu Feb 1 14:40:48 EST 2001


> In article <959ovu$sqd$1 at nnrp1.deja.com>,  <echuck at mindspring.com> wrote:
> >urllib.urlopen() can be useful for grabbing a web page and checking its
> >contents. However, JavaScript generated content throws a monkey wrench
> >in this technique.
> >
> >Has anyone cobbled together a means to invoke JavaScript from Python,
> >possibly including the DOM and the substitution of output into the HTML
> >file?

On Windows, you can use Internet Explorer via COM:

>>> import urllib
>>> print urllib.urlopen('http://localhost/javascript.html').read()
<html>
<body>
<script>
document.write('Hello, World!')
</script>
</body>
</html>
>>> from win32com.client.gencache import EnsureDispatch
>>> ie = EnsureDispatch('InternetExplorer.Application')
>>> ie.Navigate('http://localhost/javascript.html')
>>> print ie.Document.documentElement.innerHTML
<HEAD></HEAD>
<BODY>
<SCRIPT>
document.write('Hello, World!')
</SCRIPT>
Hello, World! </BODY>
>>>

--


- Geoff Talvola
  Parlance Corporation
  gtalvola at NameConnector.com





More information about the Python-list mailing list