Linux 上的 Python 和 Spidermonkey Javascript 引擎
我已经在我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我也尝试了
easy_install python-spidermonkey
但没有成功,因为libnspr-dev
包不存在。所以,我从源代码构建了包。 来自项目页面的说明 (Debian Stretch):
构建
./python-spidermonkey/trunk
CPPFLAGS="-Wno-format-security" python setup.py build
(这些标志对于 Debian)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
错误
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
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 身份安装
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:无法打开共享对象文件:没有这样的文件或目录
ImportError:无法导入名称运行时
从spidermonkey导入运行时
。原因可能是在~/.local/lib/python2.7/site-packages/spidermonkey/
中的旧 easy_install 数据中。去掉后一切顺利I also tried
easy_install python-spidermonkey
with no luck, forlibnspr-dev
package is absent.So, I've built package from source. Instructions from project page (Debian Stretch):
Building
./python-spidermonkey/trunk
CPPFLAGS="-Wno-format-security" python setup.py build
(these flags for Debian)Error
jsemit.h:508:32: error: expected ‘(’ before ‘)’ token uintN decltype);
means thatdecltype
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
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
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
, andpython setup.py install
as root.Running
libjs.so
to/usr/local/lib/
, so I didln -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
ImportError: cannot import name Runtime
afterfrom 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最近我有一个任务需要做一些类似网页抓取的事情,
对于javascript部分,目前想尝试使用 python-spidermonkey 来解决它,看看这是否对我有用......
我似乎遇到了类似的情况,在我认为我完成安装 python-spidermonkey 后,我执行上面的脚本,我得到了这个错误:
然后经过谷歌搜索...我发现解决方案可能在这里结束:
http://hi.baidu.com/peizhongyou/item/ec1575c3f0e00e31e80f2e48
我设置了这些东西:
填写这一行:
保存并保存退出,执行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:
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:
fill in this line:
save & exit, execute 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?
您需要尝试 libnspr4。如果这不起作用,您可以随时从 Mozilla 下载它并自行构建代码。
输入
./config && 并不难。制作&&解压源代码后, make install
自行构建库。如果您自己构建,文件可能会位于也可以尝试在 Google 上搜索“YOUR_OS_NAME install nspr4”。
或者在网络上搜索 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 inAlso just try Googling for "YOUR_OS_NAME install nspr4".
OR a web search for Python +AJAX might turn up exactly what you need.