PyExecJS模块
- pyexecjs是一个可以帮助我们运行js代码的一个第三方模块
- 但是它的运行是要依赖能运行js的第三方环境的
【1】 安装Nodejs
![](https://img2023.cnblogs.com/blog/2322215/202304/2322215-20230428161443689-110251031.png)
![](https://img2023.cnblogs.com/blog/2322215/202304/2322215-20230428161448032-164838054.png)
![](https://img2023.cnblogs.com/blog/2322215/202304/2322215-20230428161453343-1347347878.png)
![](https://img2023.cnblogs.com/blog/2322215/202304/2322215-20230428161455671-1909579304.png)
![](https://img2023.cnblogs.com/blog/2322215/202304/2322215-20230428161458006-705947245.png)
![](https://img2023.cnblogs.com/blog/2322215/202304/2322215-20230428161459809-1623879550.png)
![](https://img2023.cnblogs.com/blog/2322215/202304/2322215-20230428161502193-128449792.png)
![](https://img2023.cnblogs.com/blog/2322215/202304/2322215-20230428161504154-956611165.png)
![](https://img2023.cnblogs.com/blog/2322215/202304/2322215-20230428161506197-430432965.png)
【2】 安装pyexecjs
pip install pyexecjs
import execjs
print(execjs.get().name) # 需要重启pycharm或者重启电脑 Node.js (V8)
import execjs
print(execjs.get().name)
# execjs.eval 可以直接运行js代码并得到结果
js = """
"鲁班_王昭君_猴子_亚瑟_蔡文姬".split("_")
"""
res = execjs.eval(js)
print(res)
# execjs.compile(), call()
# execjs.compile() 事先加载好一段js代码,
jj = execjs.compile("""
function an(a, b){
return a + b
}
""")
# call() 运行代码中的xxx函数. 后续的参数是xxx的参数
ret = jj.call("an", 10, 20)
print(ret)
- windows中如果出现编码错误. 在引入execjs之前. 插入以下代码即可.
import subprocess
from functools import partial
subprocess.Popen = partial(subprocess.Popen, encoding='utf-8')
import execjs
- 完事儿. 你没有看错. execjs就这几个功能就够咱用的了.