安装插件
pip install pyexecjs
直接安装可能会出错:
C:\Users\Administrator>pip install execjs
Collecting execjs
Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError(SSLError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:852)'),)': /simple/execjs/
解决方法:引用国内的镜像安装
pip install pyexecjs -i http://pypi.douban.com/simple --trusted-host pypi.douban.com
加载JS并执行
import execjs
def get_js_txt(path): #读取js文件用于加载js
f = open(path, 'r', encoding='utf-8')
line = f.readline()
js_str = ''
while line:
js_str += line
line = f.readline()
return js_str
def run_js():
js_str = get_js_txt(path)
ctx = execjs.compile(js_str)
return (ctx.call('js_func', pramas)) # 第一个参数js_func为要调用的js函数方法,后面pramas就是函数的参数,多个以逗号隔开