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?
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):
./python-spidermonkey/trunk
CPPFLAGS="-Wno-format-security" python setup.py build
(these flags for Debian)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
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
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
su
, and python setup.py install
as root.
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
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