javascriptpythonlinuxspidermonkey

Python and the Spidermonkey Javascript engine on Linux


I have successfully installed Spidermonkey JS engine on my Linux machine ( Ubuntu ). Basically my goal is to make it execute Ajax (js) scripts and return the result back to my Python script. I'm basically trying to build a good O.O. web scraper. But it's pretty hard for me to get all of this working.

I'm now at the point where when I type JS in my terminal I can start executing Javascript. I've been Googling and found this little snipet on Stackoverflow :

import urllib2
import spidermonkey
js = spidermonkey.Runtime()
js_ctx = js.new_context()
script = urllib2.urlopen('http://etherhack.co.uk/hashing/whirlpool/js/whirlpool.js').read()
js_ctx.eval_script(script)
js_ctx.eval_script('var s="abc"')
js_ctx.eval_script('print(HexWhirpool(s))')

but it failed to run with the error that module Spidermonkey can not be found.

I'm a bit lost now. Anyone able to help?


Solution

  • I also tried easy_install python-spidermonkey with no luck, for libnspr-dev package is absent.

    So, I've built package from source. Instructions from project page (Debian Stretch):

    Building

    1. Check out the Python-Spidermonkey module from the SVN repository ( I downloaded it as source archive, direct link )
    2. Unpack, and cd to ./python-spidermonkey/trunk
    3. CPPFLAGS="-Wno-format-security" python setup.py build (these flags for Debian)
    4. Error jsemit.h:508:32: error: expected ‘(’ before ‘)’ token uintN decltype); means that decltype cannot be used as variable (maybe it's a macro or something else), fix it this way:

      sed -e 's/decltype/dectyp/' -i.ORIG ./js/src/jsemit.h

      sed -e 's/decltype/dectyp/' -i.ORIG ./js/src/jsemit.cpp

    5. Error jsemit.cpp:6490:1: error: narrowing conversion of ‘-1’ from ‘int’ to ‘uint8 {aka unsigned char}’ inside { } [-Wnarrowing] means illegal variable conversion, recompile it manually:

      cd js/src

      g++ -o Linux_All_DBG.OBJ/jsemit.o -c -Wall -Wno-narrowing -Wno-format -MMD -g3 -DXP_UNIX -DSVR4 -DSYSV -D_BSD_SOURCE -DPOSIX_SOURCE -DHAVE_LOCALTIME_R -DHAVE_VA_COPY -DVA_COPY=va_copy -DPIC -fPIC -DDEBUG -DDEBUG_user -DEDITLINE -ILinux_All_DBG.OBJ jsemit.cpp

    6. Error spidermonkey.c:1:2: error: #error Do not use this file, it is the result of a failed Pyrex compilation. - some trouble with pyrex. There is a patch. Do it this way:

      wget -O - https://storage.googleapis.com/google-code-attachments/python-spidermonkey/issue-14/comment-4/cinit.patch | patch -p1 ./spidermonkey.pyx

    Installation

    su, and python setup.py install as root.

    Running

    1. By default, setup script installs libjs.so to /usr/local/lib/, so I did ln -s /usr/local/lib/libjs.so /usr/lib/libjs.so (but you'd better use solution from Seagal82)

    Without this step, python keeps complaining about import ImportError: libjs.so: cannot open shared object file: No such file or directory

    1. I also had an error ImportError: cannot import name Runtime after from spidermonkey import Runtime. The reason possibly was in old easy_install data in ~/.local/lib/python2.7/site-packages/spidermonkey/. After removing it, all runs smooth