赞
踩
pip install PyExecJS
PyExecJS 示例
import execjs
jsstr = '''
function add() {
let a = 1;
let b = 2;
return a+b;
}'''
js = execjs.compile(jsstr)
result = js.call('add')
# call 里面的参数第一个是js函数名称, 如果要执行的js有参数, 后面跟上参数就可以
# 如js.call('add', 2, 3)这样
print(result)
# 安装Pyv8,下砸对应的二进制版本
# https://github.com/emmetio/pyv8-binaries
# PyV8 是V8引擎的Python语言封装697以上star
PyV8 示例
import PyV8
ctxt = PyV8.JSContext()
# ctxt.__enter__()
ctxt.enter()
jsstr = '''
function add() {
let a = 1;
let b = 2;
return a+b;
}'''
result = ctxt.eval(jsstr)
print(result)
pip install js2py
js2py 示例
import js2py
jsstr = '''
function add() {
let a = 1;
let b = 2;
return a+b;
}'''
content = js2py.EvalJs() # 实例化解析js对象
content.execute(jsstr) # js转python代码
result = content.add()
print(result)
pip install selenium
selenium 示例
from selenium import webdriver
jsstr = '''
function add() {
let a = 1;
let b = 2;
return a+b;
}'''
# 调用js
driver = webdriver.chrome()
# driver.execute_async_script(js)
result = driver.execute_script(jsstr)
print(result)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。