import execjs
#读取js文件
with open('t.js',encoding='utf-8') as f:
js = f.read()
#通过compile命令转成一个js对象
docjs = execjs.compile(js)
#调用function
res = docjs.call('createGuid')
print(res)
#调用变量
res = docjs.eval('guid')
print(res)
输出结果:
2e45
1efdfa41-4450-0302c2a6-fb959feef6a4
t.js 文件:
var createGuid = function() {
return (((1 + Math.random()) * 65536) | 0).toString(16).substring(1)
};
var guid = createGuid() + createGuid() + "-" + createGuid() + "-" + createGuid() + createGuid() + "-" + createGuid() + createGuid() + createGuid();
python2执行执行js文件使用pyv8