【问题标题】:Python and the Spidermonkey Javascript engine on LinuxLinux 上的 Python 和 Spidermonkey Javascript 引擎
【发布时间】:2012-03-13 06:50:27
【问题描述】:

我已经在我的 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 的错误。

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

【问题讨论】:

  • 你是否也安装了这个:code.google.com/p/python-spidermonkey
  • 是的,我做了:easy_install python-spidermonkey,但它返回一个错误:RuntimeError: No package configuration found for: nspr 试图通过安装来修复该错误:apt-get install libnspr-dev pkg-config got此错误:包 libnspr-dev 不可用,但被另一个包引用。这可能意味着该软件包丢失、已过时或仅可从其他来源获得 E:软件包“libnspr-dev”没有安装候选者并且现在正式卡住了
  • 另一种方法是使用 Python 的 QtWebKit + PySide 绑定——我已经取得了巨大的成功。您还将对 HTML 和 Javascript 交互进行更全面的处理,因为它将在“真实浏览器中”运行。浏览器会进行一些数据处理,以确保无效但“非常接近”的 HTML 仍能正确呈现,手动执行此操作要困难得多。这个解决方案比你想要的要重得多,但我现在不会这样做。
  • “不相信所有的说法都是一种真实的方式” - Unix 哲学 ;)

标签: javascript python linux spidermonkey


【解决方案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 提供的脚本 不知道这是否是您需要的答案,还是还有帮助?

【讨论】:

    【解决方案2】:

    我也尝试了easy_install python-spidermonkey,但没有成功,因为libnspr-dev 包不存在。

    所以,我已经从源代码构建了包。 Instructions from project page(Debian Stretch):

    建筑

    1. 从 SVN 存储库中查看 Python-Spidermonkey 模块(我将其作为源存档下载,direct link
    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. - 耐热玻璃有一些问题。有a patch。这样做:

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

    安装

    supython setup.py install 作为 root。

    跑步

    1. 默认情况下,安装脚本安装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

    1. from spidermonkey import Runtime 之后我也遇到了错误ImportError: cannot import name Runtime。原因可能是~/.local/lib/python2.7/site-packages/spidermonkey/ 中的旧easy_install 数据。去掉后,一切顺利

    【讨论】:

      【解决方案3】:

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

      解压源码后,输入./config &amp;&amp; make &amp;&amp; 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-02-12
        • 2012-05-15
        • 1970-01-01
        • 2011-04-28
        • 1970-01-01
        • 1970-01-01
        • 2012-09-01
        • 2021-01-10
        • 1970-01-01
        相关资源
        最近更新 更多