python调用js

安装

pip install PyExecJS

方法

eval()
    输入参数:source(JS语句)、cwd(路径)
    返回值:result(语句执行结果)

compile()
    输入参数:source(JS语句)、cwd(路径)
    返回值:编译后的对象
call()
    输入参数:name(要调用的JS方法名称)、*args(方法所需要的参数,可多个)
    返回值:result(运行方法的返回结果)

模板

执行JS语句

import execjs
result=execjs.eval("1+1")
print(result)

调用JS方法

getUID = """
function getUID() {
	function a(a, b) {
		var e = (b || 2) - (1 + Math.floor(Math.log(a | 1) / Math.LN10 + 1E-15));
		return Array(e + 1).join("0") + a
	}
	var b = new Date;
	return "" + a(b.getHours()) + a(b.getMinutes()) + a(b.getSeconds()) + a(b.getMilliseconds(), 3) + a(Math.ceil(9999 * Math.random()), 4)
}
"""

import execjs
js=execjs.compile(getUID)
result=js.call('getUID')
print(result)
posted @ 2020-03-09 20:35  sfdevs  阅读(393)  评论(0)    收藏  举报