knighterrant

执行JS的类库:execjs,PyV8,selenium,node

execjs是一个比较好用且容易上手的类库(支持py2,与py3),支持 JS runtime。

1.安装:

pip install PyExecJS

2.运行环境

execjs会自动使用当前电脑上的运行时环境(建议用nodejs,与Phantomjs)

execjs.get().name

通过运行时环境运行js:

default = execjs.get()

default.eval("1 + 2")

1.例子1:

import execjs

execjs.eval("\'red yellow blue\'.split(\' \')")

 

结果:

[\'red\', \'yellow\', \'blue\']

 

案例:

default = execjs.get()  # ExternalRuntime(Node.js (V8))

res=default.eval("1 + 2")

print(res)   # 3

 

2.例子2:

ctx = execjs.compile(

\'\'\'

function add(x, y) { ... return x + y; ... }

\'\'\'

)

ctx.call("add", 1, 2)

结果 : 3

 

分类:

技术点:

相关文章:

  • 2021-12-04
  • 2021-07-25
  • 2022-01-22
  • 2021-07-18
  • 2021-09-25
  • 2022-01-21
  • 2022-01-21
猜你喜欢
  • 2022-12-23
  • 2021-12-11
  • 2021-11-23
  • 2022-02-10
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案