思路:在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腳本的交互