在nodejs的语法中运行python的脚本
思路:在nodejs调用本地的cmd命令,通过cmd命令执行唤起python解析器,用python命令去执行python脚本;
node的参考链接:
http://nodejs.cn/api/child_process.html
child_process
模块有两个方法。分别是exec
和execSync
,分别表示异步和同步,
异步实现:
const pro = require("child_process")
pro.exec("python url.py", function (error, stdout, stderr) {
if (error) {
console.info("stderr:" + stderr)
}
console.log("exec:" + stdout)
})
同步实现:
const py = pro.execSync("python url.py")
console.log(py.toString())
这样就实现了Nodejs与Python脚本的交互