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.
it's not yet possible to call a function defined in Javascript
)Edit: An example will be great.. (barebones will do)
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.
You can combine JavaScript and Python without using a database or developing a cumbersome API structure to improve development efficiency.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With