How do I call a Javascript function from Python?

Question:

I am working on a web-scraping project. One of the websites I am working with has the data coming from Javascript.

There was a suggestion on one of my earlier questions that I can directly call the Javascript from Python, but I’m not sure how to accomplish this.

For example: If a JavaScript function is defined as: add_2(var,var2)

How would I call that JavaScript function from Python?

Asked By: Kiran

||

Answers:

Find a JavaScript interpreter that has Python bindings. (Try Rhino? V8? SeaMonkey?). When you have found one, it should come with examples of how to use it from python.

Python itself, however, does not include a JavaScript interpreter.

To interact with JavaScript from Python I use webkit, which is the browser renderer behind Chrome and Safari. There are Python bindings to webkit through Qt. In particular there is a function for executing JavaScript called evaluateJavaScript().

Here is a full example to execute JavaScript and extract the final HTML.

Answered By: hoju

You can eventually get the JavaScript from the page and execute it through some interpreter (such as v8 or Rhino). However, you can get a good result in a way easier way by using some functional testing tools, such as Selenium or Splinter. These solutions launch a browser and effectively load the page – it can be slow but assures that the expected browser displayed content will be available.

For example, consider the HTML document below:

<html>
    <head>
        <title>Test</title>
        <script type="text/javascript">
            function addContent(divId) {
                var div = document.getElementById(divId);
                div.innerHTML = '<em>My content!</em>';
            }
        </script>
    </head>
    <body>
        <p>The element below will receive content</p>
        <div id="mydiv" />
        <script type="text/javascript">addContent('mydiv')</script>
    </body>
</html>

The script below will use Splinter. Splinter will launch Firefox and after the complete load of the page it will get the content added to a div by JavaScript:

from splinter.browser import Browser
import os.path

browser = Browser()
browser.visit('file://' + os.path.realpath('test.html'))
elements = browser.find_by_css("#mydiv")
div = elements[0]
print div.value

browser.quit()

The result will be the content printed in the stdout.

Answered By: brandizzi

An interesting alternative I discovered recently is the Python bond module, which can be used to communicate with a NodeJs process (v8 engine).

Usage would be very similar to the pyv8 bindings, but you can directly use any NodeJs library without modification, which is a major selling point for me.

Your python code would look like this:

val = js.call('add2', var1, var2)

or even:

add2 = js.callable('add2')
val = add2(var1, var2)

Calling functions though is definitely slower than pyv8, so it greatly depends on your needs. If you need to use an npm package that does a lot of heavy-lifting, bond is great. You can even have more nodejs processes running in parallel.

But if you just need to call a bunch of JS functions (for instance, to have the same validation functions between the browser/backend), pyv8 will definitely be a lot faster.

Answered By: user4038045

Did a whole run-down of the different methods recently.

PyQt4
node.js/zombie.js
phantomjs

Phantomjs was the winner hands down, very straightforward with lots of examples.

Answered By: frage

You might call node through Popen.

My example how to do it

print execute('''function (args) {
    var result = 0;
    args.map(function (i) {
        result += i;
    });
    return result;
}''', args=[[1, 2, 3, 4, 5]])
Answered By: mosquito

Hi so one possible solution would be to use ajax with flask to comunicate between javascript and python. You would run a server with flask and then open the website in a browser. This way you could run javascript functions when the website is created via pythoncode or with a button how it is done in this example.

HTML code:



<html>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>


<script>
    function pycall() {
      $.getJSON('/pycall', {content: "content from js"},function(data) {
          alert(data.result);
      });
    }
</script>


<button type="button" onclick="pycall()">click me</button>
 

</html>

Python Code:

from flask import Flask, jsonify, render_template, request

app = Flask(__name__)


def load_file(file_name):
    data = None
    with open(file_name, 'r') as file:
        data = file.read()
    return data

@app.route('/pycall')
def pycall():
    content = request.args.get('content', 0, type=str)
    
    print("call_received",content)
    return jsonify(result="data from python")

@app.route('/')
def index():
    return load_file("basic.html")



import webbrowser
print("opening localhost")
url = "http://127.0.0.1:5000/"
webbrowser.open(url)
app.run()

output in python:

call_received content from js

alert in browser:

data from python

Answered By: rtuiiop

This worked for me for simple js file, source:
https://www.geeksforgeeks.org/how-to-run-javascript-from-python/

pip install js2py
pip install temp

file.py

import js2py
eval_res, tempfile = js2py.run_file("scripts/dev/test.js")
tempfile.wish("GeeksforGeeks")

scripts/dev/test.js

function wish(name) {
    console.log("Hello, " + name + "!")
}
Answered By: werkhardor
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.