我已经在我的 Linux 机器 (Ubuntu) 上成功安装了 Spidermonkey JS 引擎。 基本上我的目标是让它执行 Ajax (js) 脚本并将结果返回给我的 Python 脚本。我基本上是在尝试建立一个好的 O.O.网络抓取工具。但是我很难让所有这些工作。
现在,当我在终端中输入 JS 时,我就可以开始执行 Javascript。 我一直在谷歌搜索并在 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))')
但是运行失败,提示找不到模块Spidermonkey。
我现在有点迷路了。有谁能帮忙吗?
最佳答案
我也试过easy_install python-spidermonkey
没有运气,libnspr-dev
包裹不存在。
所以,我从源代码构建了包。 Instructions from project page (Debian 延伸):
建筑
- 从 SVN 存储库中查看 Python-Spidermonkey 模块(我将其下载为源存档,direct link)
- 解压缩,然后 cd 到
./python-spidermonkey/trunk
-
CPPFLAGS="-Wno-format-security" python setup.py build
(这些标志用于 Debian) 错误
jsemit.h:508:32: error: expected ‘(’ before ‘)’ token uintN decltype);
意味着decltype
不能用作变量(可能是宏或其他东西),这样修复:sed -e 's/decltype/dectyp/' -i.ORIG ./js/src/jsemit.h
sed -e 's/decltype/dectyp/' -i.ORIG ./js/src/jsemit.cpp
错误
jsemit.cpp:6490:1: error: narrowing conversion of ‘-1’ from ‘int’ to ‘uint8 {aka unsigned char}’ inside { } [-Wnarrowing]
表示非法变量转换,手动重新编译:cd js/源代码
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
错误
spidermonkey.c:1:2: error: #error Do not use this file, it is the result of a failed Pyrex compilation.
- 派热克斯玻璃有些问题。有 a patch .这样做:wget -O - https://storage.googleapis.com/google-code-attachments/python-spidermonkey/issue-14/comment-4/cinit.patch |补丁-p1 ./spidermonkey.pyx
安装
su
, 和 python setup.py install
作为根。
运行
- 默认情况下,安装脚本会安装
libjs.so
至/usr/local/lib/
,所以我做了ln -s /usr/local/lib/libjs.so /usr/lib/libjs.so
(但你最好使用 solution from Seagal82 )
如果没有这一步,python 会一直报错 import ImportError: libjs.so: cannot open shared object file: No such file or directory
- 我也有一个错误
ImportError: cannot import name Runtime
在from spidermonkey import Runtime
之后.原因可能是在旧的 easy_install 数据中~/.local/lib/python2.7/site-packages/spidermonkey/
.删除它后,一切运行顺利
关于javascript - Linux 上的 Python 和 Spidermonkey Javascript 引擎,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9403337/