Linux 上的 Python 和 Spidermonkey Javascript 引擎

发布于 2025-01-07 23:57:58 字数 616 浏览 1 评论 0原文

我已经在我的 Linux 机器(Ubuntu)上成功安装了 Spidermonkey JS 引擎。 基本上我的目标是让它执行 Ajax (js) 脚本并将结果返回到我的 Python 脚本。我基本上是在尝试构建一个优秀的面向对象网络抓取工具。但让所有这些工作对我来说非常困难。

现在,当我在终端中输入 JS 时,我就可以开始执行 Javascript 了。 我一直在 Google 上搜索并在 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 的错误。

我现在有点迷失了。有人能帮忙吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

紫南 2025-01-14 23:57:58

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

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

构建

  1. 查看 Python-来自 SVN 存储库的 Spidermonkey 模块(我将其作为源存档下载,直接链接 )
  2. 解压,然后 cd 到 ./python-spidermonkey/trunk
  3. CPPFLAGS="-Wno-format-security" python setup.py build (这些标志对于 Debian)
  4. Error 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:错误:将“-1”从“int”缩小到“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-DDBUG -DDEBUG_user -DEDITLINE -ILinux_All_DBG.OBJ jsemit.cpp

  6. Error spidermonkey.c:1:2: error: #error 不要使用此文件,它是 Pyrex 编译失败的结果。 - 出现一些问题耐热玻璃。有补丁。这样做:

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

以 root 身份安装

supython setup.py install

运行

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

)这步骤,python不断抱怨导入ImportError:libjs.so:无法打开共享对象文件:没有这样的文件或目录

  1. 之后我也遇到了错误ImportError:无法导入名称运行时 从spidermonkey导入运行时。原因可能是在 ~/.local/lib/python2.7/site-packages/spidermonkey/ 中的旧 easy_install 数据中。去掉后一切顺利

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):

Building

  1. Check out the Python-Spidermonkey module from the SVN repository ( I downloaded it as source archive, direct link )
  2. Unpack, and cd to ./python-spidermonkey/trunk
  3. CPPFLAGS="-Wno-format-security" python setup.py build (these flags for Debian)
  4. 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

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

  6. 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

Installation

su, and python setup.py install as root.

Running

  1. By default, setup script installs 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

  1. I also had an error 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
可爱暴击 2025-01-14 23:57:58

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

Recently i got a task need to do something like Web scraping,
and for the javascript part, currently want to try using python-spidermonkey to resolve it and see if this might work for me ...

and i seem to meet situation might alike, after i think i finished install python-spidermonkey, i execute the script above, i got this error:

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

then after some searching by google...i found the solution probably in the end of here:
http://hi.baidu.com/peizhongyou/item/ec1575c3f0e00e31e80f2e48

i setup these things:

$sudo vi /etc/ld.so.conf.d/libjs.so.conf

fill in this line:

/usr/local/lib/

save & exit, execute ldconfig:

$sudo ldconfig

then i can run the script provided above by @Synbitz Prowduczions
don't know if this is the answer you need, or this still helps?

欢你一世 2025-01-14 23:57:58

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

输入 ./config && 并不难。制作&&解压源代码后, make install 自行构建库。如果您自己构建,文件可能会位于

/usr/local/{include,lib}

也可以尝试在 Google 上搜索“YOUR_OS_NAME install nspr4”。

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

或者在网络上搜索 Python +AJAX 可能会找到您所需要的内容。

You need to try libnspr4. If that doesn't work, you can always download it from Mozilla and build the code yourself.

It is not difficult to type ./config && make && make install to build the library yourself after untarring the source. If you build yourself, files will likely be in

/usr/local/{include,lib}

Also just try Googling for "YOUR_OS_NAME install nspr4".

  • I believe someone wrote a C/C++ header file translator for Python ctypes. Although I can't say much else because I don't use Python.
  • SpiderMonkey also has its own implementation of ctypes modeled after Python. So technically if you know javascript you could forego using Python altogether since you want to do some ajax with it. You will need to brush up on the NSPR or C runtime sockets to meet the requirements for your projects using only Spidermonkey.

OR a web search for Python +AJAX might turn up exactly what you need.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文