python的execjs执行js代码

安装
# 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
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
发布了64 篇原创文章 · 获赞 26 · 访问量 36万+

猜你喜欢

转载自blog.csdn.net/moliyiran/article/details/103711925