PyExecJS 的使用
安装 JS 运行环境推荐安装 Node.js,安装方便,执行效率也高。
Test:
1 2 3 4
| In [1]: import execjs
In [2]: execjs.get().name # 查看调用环境 Out[2]: 'Node.js (V8)'
|
如果你检测出来的引擎不是 node.js
的话,那你就需要手动设置一下了,这里有两种设置形式: 选择不同引擎进行解析
1 2 3 4 5 6
| os.environ["EXECJS_RUNTIME"]="Node"
import execjs.runtime_names node=execjs.get(execjs.runtime_names.Node)
|
demo
1 2 3 4 5 6 7 8 9
| import execjs jstext = """ function hello(str){return str;} """
ctx = execjs.compile(jstext) a = ctx.call("hello", "hello aiyc") print(a)
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| from pprint import pprint
import execjs import pathlib import os
js_path = pathlib.Path(os.path.abspath(os.path.dirname(__file__))) js_path = js_path / "crypto.js"
with js_path.open('r', encoding="utf-8") as f: script = f.read()
c = "1234"
add = (''' aesEncrypt = function() { result={} var t = CryptoJS.MD5("login.xxx.com"), i = CryptoJS.enc.Utf8.parse(t), r = CryptoJS.enc.Utf8.parse("1234567812345678"), u = CryptoJS.AES.encrypt(''' + "'{}'".format(c) + ''',i, { iv: r }); result.t=t.toString() result.i =i.toString() result.r =r.toString() result.u =u.toString() return result }; ''') script = script + add print("script",script)
x = execjs.compile(script) result = x.call("aesEncrypt") print(result)
|
PyExecJS 存在的一些问题
执行大型 JS 时会有点慢(这个是因为,每次执行 JS 代码的时候,都是从命令行去调用到的 JS,所以 JS 代码越复杂的话,nodejs 的初始化时间就越长,这个基本上是无解的)
特殊编码的输入或输出参数会出现报错的情况(因为,是从命令行调用的,所以在碰到一些特殊字符输入或输出参数或者 JS 代码本身就有一些特殊字符的情况下,就会直接执行不了,给你抛出一个异常。不过这个跟系统的命令行默认编码有一定关系,具体的话这里就不深究了,直接就说解决方案吧。)
可以把输入或输出的参数使用 Base64 编码一下(如果看报错是 JS 代码部分导致的,那就去看看能不能删除代码中的那部分字符或者你自己 new 一个上下文对象,将那个名叫 tempfile 的参数打开,这样在调用的时候,它就直接去执行那个文件了,不过大量调用的情况下,可能会对磁盘造成一定压力。
其他使用 Python 调用 JS 的骚操作
Selenium
1 2
| js = "一大段 JS" result = browser.execute_script(js)
|
reference