Python爬虫:execjs在python中运行javascript代码

execjs可以在python中运行javascript代码

安装

$ pip install PyExecJS

示例

import execjs

execjs.eval("new Date()")
# u'2018-09-08T09:11:35.248Z'

js = """
function add(x, y){
    return x + y;
}
"""
ctx = execjs.compile(js)
ctx.call("add", 3, 4)
# 7

备注:
设备上需要有执行js的程序才行,比如

  • PyV8 - A python wrapper for Google V8 engine,
  • Node.js
  • PhantomJS
  • Nashorn - Included with Oracle Java 8

参考:
https://pypi.org/project/PyExecJS/

你可能感兴趣的:(python)