0

我已经在我的 Linux 机器(Ubuntu)上成功安装了 Spidermonkey JS 引擎。基本上我的目标是让它执行 Ajax (js) 脚本并将结果返回给我的 Python 脚本。我基本上是在尝试构建一个好的 OO 网络爬虫。但我很难让所有这些工作。

我现在可以在终端中键入 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 的错误。

我现在有点迷路了。有谁能帮忙吗?

4

3 回答 3

1

最近我有一个任务需要做一些像网页抓取这样的事情,对于 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 提供的脚本,不知道这是否是您需要的答案,或者这仍然有帮助吗?

于 2013-05-10T08:34:09.100 回答
1

我也试过easy_install python-spidermonkey没有运气,因为没有libnspr-dev包裹。

所以,我已经从源代码构建了包。项目页面的说明(Debian Stretch):

建造

  1. 从 SVN 存储库中查看 Python-Spidermonkey 模块(我将其下载为源存档,直接链接
  2. 解压,然后 cd 到./python-spidermonkey/trunk
  3. CPPFLAGS="-Wno-format-security" python setup.py build(Debian 的这些标志)
  4. 错误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

  5. 错误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

  6. 错误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作为根。

跑步

  1. 默认情况下,安装脚本安装libjs.so/usr/local/lib/,所以我做了ln -s /usr/local/lib/libjs.so /usr/lib/libjs.so(但你最好使用Seagal82 的解决方案

没有这一步,python一直在抱怨importImportError: libjs.so: cannot open shared object file: No such file or directory

  1. ImportError: cannot import name Runtime之后我也有错误from spidermonkey import Runtime。原因可能是在旧的 easy_install 数据中~/.local/lib/python2.7/site-packages/spidermonkey/。拆下后一切顺利
于 2016-05-16T13:27:10.327 回答
0

您需要尝试 libnspr4。如果这不起作用,您可以随时从 Mozilla 下载它并自己构建代码。

./config && make && make install解压源码后,自己键入构建库并不难。如果您自己构建,文件可能会在

/usr/local/{include,lib}

也只需尝试谷歌搜索“YOUR_OS_NAME install nspr4”。

  • 我相信有人为 Python ctypes 编写了 C/C++ 头文件翻译器。虽然我不能说太多,因为我不使用 Python。
  • SpiderMonkey 也有自己的以 Python 为模型的 ctypes 实现。所以从技术上讲,如果你知道 javascript,你可以完全放弃使用 Python,因为你想用它做一些 ajax。您将需要复习 NSPR 或 C 运行时套接字以满足仅使用 Spidermonkey 的项目的要求。

或者,对 Python +AJAX 的网络搜索可能会完全满足您的需求。

于 2012-03-31T08:35:01.603 回答