Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Executing Javascript from Python

I have HTML webpages that I am crawling using xpath. The etree.tostring of a certain node gives me this string:

<script> <!-- function escramble_758(){   var a,b,c   a='+1 '   b='84-'   a+='425-'   b+='7450'   c='9'   document.write(a+c+b) } escramble_758() //--> </script> 

I just need the output of escramble_758(). I can write a regex to figure out the whole thing, but I want my code to remain tidy. What is the best alternative?

I am zipping through the following libraries, but I didnt see an exact solution. Most of them are trying to emulate browser, making things snail slow.

  • http://code.google.com/p/python-spidermonkey/ (clearly says it's not yet possible to call a function defined in Javascript)
  • http://code.google.com/p/webscraping/ (don't see anything for Javascript, I may be wrong)
  • http://pypi.python.org/pypi/selenium (Emulating browser)

Edit: An example will be great.. (barebones will do)

like image 935
jerrymouse Avatar asked Apr 13 '12 06:04

jerrymouse


People also ask

Can you run JavaScript from Python?

JS2PY works by translating JavaScript directly into Python. It indicates that you may run JS directly from Python code without installing large external engines like V8. To use the module it first has to be installed into the system, since it is not built-in.

Can Python and JavaScript work together?

You can combine JavaScript and Python without using a database or developing a cumbersome API structure to improve development efficiency.

Can Python communicate with JavaScript?

Are you wondering how you can send data from JavaScript to Python? Well, that's easy with an application programming interface (API). Programming languages communicate and exchange data using APIs.


1 Answers

You can also use Js2Py which is written in pure python and is able to both execute and translate javascript to python. Supports virtually whole JavaScript even labels, getters, setters and other rarely used features.

import js2py  js = """ function escramble_758(){ var a,b,c a='+1 ' b='84-' a+='425-' b+='7450' c='9' document.write(a+c+b) } escramble_758() """.replace("document.write", "return ")  result = js2py.eval_js(js)  # executing JavaScript and converting the result to python string  

Advantages of Js2Py include portability and extremely easy integration with python (since basically JavaScript is being translated to python).

To install:

pip install js2py 
like image 166
Piotr Dabkowski Avatar answered Sep 22 '22 08:09

Piotr Dabkowski