我已经成功地在我的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))')
但它无法运行,并出现找不到模块蜘蛛猴的错误。
我现在有点迷茫。有人能帮忙吗?
我也尝试easy_install python-spidermonkey
但没有运气,因为libnspr-dev
包不存在。
所以,我从源代码构建了包。来自项目页面的说明(Debian Stretch
(:建筑
- 从SVN存储库中查看Python-Spidermonkey模块(我将其下载为源存档,直接链接(
- 打开包装,然后 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/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
错误
spidermonkey.c:1:2: error: #error Do not use this file, it is the result of a failed Pyrex compilation.
- 耐热玻璃有些麻烦。有一个补丁。这样做: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
(但你最好使用 Seagal82 的解决方案(
如果没有这一步,python 一直在抱怨导入ImportError: libjs.so: cannot open shared object file: No such file or directory
from spidermonkey import Runtime
后我也ImportError: cannot import name Runtime
错误.原因可能是在~/.local/lib/python2.7/site-packages/spidermonkey/
的旧easy_install数据中。删除后,一切运行顺利
最近我有一个任务需要做一些类似网页抓取的事情,对于 JavaScript 部分,目前想尝试使用 python-spidermonkey 来解决它,看看这是否对我有用......
而且我似乎遇到了类似的情况,在我认为我完成了 python-spidermonkey 的安装后,我执行了上面的脚本,我得到了这个错误:
Traceback (most recent call last):
File "spidermonkeytest.py", line 2, in <module>
import spidermonkey
ImportError: libjs.so: cannot open shared object file: No such file or directory
然后经过谷歌的一些搜索...我可能在这里结束时找到了解决方案:http://hi.baidu.com/peizhongyou/item/ec1575c3f0e00e31e80f2e48
我设置了这些东西:
$sudo vi /etc/ld.so.conf.d/libjs.so.conf
填写此行:
/usr/local/lib/
保存并退出,执行 ldconfig:
$sudo ldconfig
然后我可以运行上面提供的脚本 @Synbitz Prowduczions不知道这是您需要的答案,还是仍然有帮助?
你需要尝试libnspr4。如果这不起作用,您可以随时从 Mozilla 下载它并自己构建代码。
解压缩源代码后,键入./config && make && make install
自己构建库并不困难。如果您自己构建,文件可能会位于
/usr/local/{include,lib}
也只需尝试谷歌搜索"YOUR_OS_NAME安装 nspr4"。
- 我相信有人为 Python ctypes 编写了一个 C/C++ 头文件转换器。虽然我不能说太多,因为我不使用Python。
- SpiderMonkey也有自己的ctypes实现,以Python为模型。所以从技术上讲,如果你知道javascript,你可以完全放弃使用Python,因为你想用它做一些ajax。您需要复习 NSPR 或 C 运行时套接字,以满足仅使用 Spidermonkey 的项目要求。
或者在网上搜索Python + AJAX可能会找到你需要的。