2

I have a python script and this python script shall call an html-file (i.e. a web page) stored locally on the computer. The html-file does some calculations (jquery,javascript and so on) and should pass the result back to the python script. I don't want to change the setting (python script calls html-file and result is passed back to python-script) so please don't ask why.

Could anyone tell me how to solve this? How can I pass the result from html-file to the calling python function? That troubles me since 2 weeks. Thanks!

Paul D. Waite
  • 96,640
  • 56
  • 199
  • 270
manton
  • 541
  • 1
  • 6
  • 26

4 Answers4

3

If you want to execute the javascript in the HTML file, you will need to implement a full javascript runtime in Python (there might be existing ones or ports from other languages of course.) An HTML file is not an executable, and most environments don't have an interpreter for it either, except browsers.

If you can separate the javascript from the HTML somehow (that is, if it doesn't depend on the DOM in any way) you might be able to use something like the SpiderMonkey shell to execute your javascript and read it's standard output in Python.

If not, you will need a full browser to interpret the HTML and javascript, and you will then need to read it's (visual) output somehow, which might be incredibly complex.

richardolsson
  • 4,041
  • 1
  • 17
  • 19
1

http://nodejs.org/
Remove anything to do with the dom and run it from command with node then call the Python script from node with the out put from the JS and then just pipe the out put some where.

Tegra Detra
  • 24,551
  • 17
  • 53
  • 78
0

This would be very hard to accomplish without the use of external libraries. You'd need a HTML parser to start with, so you can actually make sense of the HTML. Then you'd need a Javascript parser/lexer/engine so you could do the actual calculations. I guess it would be possible to implement this in Python, but I'd recommend looking for an open source project which already implemented this. You'd then have to parse/lex/interpret the javascript and pass back the result to python.

All in all I'd say it's easier to just port the Javascript calculation to Python, but that's just me.

Exelian
  • 5,749
  • 1
  • 30
  • 49
0

You haven’t specified exactly what you mean when you say your Python script “calls” the HTML file, but presuming you mean that Python gets the contents of the HTML file, you’re going to need Python to interpret and run the JavaScript therein to start with.

I wrote a Python script that logs onto my internet banking provider (which uses JavaScript heavily), and I ended up having to use Selenium to control an actual web browser to parse the JavaScript for me, and then query the DOM of the rendered web page via Selenium. It was decidedly non-trivial.

The answers to my question mention a couple of HTML parsers for Python. I’d agree with James’ answer though — get the JavaScript from the HTML file and run it with node.js.

Community
  • 1
  • 1
Paul D. Waite
  • 96,640
  • 56
  • 199
  • 270