安裝
# python3安裝
pip install PyExecJS
# python2安裝
pip install pyexecjs
execjs執行語法
import execjs
jsFunc = '''
function add(x,y){
return x+y;
}
'''
jscontext = execjs.compile(jsFunc)
a = jscontext.call('add',3,5)
print(a)
# 可識別字符串,元組,字典,列表等
python中調用js文件使用js方法
1,首先通過,get_js方法,讀取本地的 des_rsa.js 文件。2,調用 execjs.compile() 編譯並加載 js 文件內容。3,使用call()調用js中的方法
import execjs
#執行本地的js
def get_js():
# f = open("D:/WorkSpace/MyWorkSpace/jsdemo/js/des_rsa.js",'r',encoding='UTF-8')
f = open("./js/des_rsa.js", 'r', encoding='UTF-8')
line = f.readline()
htmlstr = ''
while line:
htmlstr = htmlstr + line
line = f.readline()
return htmlstr
jsstr = get_js()
ctx = execjs.compile(jsstr)
print(ctx.call('enString','123456'))
作者:tkpy
鏈接:https://www.jianshu.com/p/df4ae2374a68
來源:簡書
著作權歸作者所有。商業轉載請聯繫作者獲得授權,非商業轉載請註明出處。