在PyV8中使用DOM文档

2024-09-28 03:16:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要在Python中运行javascript代码。这个JS代码使用documentDOM对象函数,我不知道如何在JS代码中“创建DOM对象”来使用它。在

目前我调用的JS文件如下:

import json
import PyV8

def runJS(html):
    ctxt = PyV8.JSContext()
    ctxt.enter()
    ctxt.eval(open('JScode.js').read().decode('utf8'))
    document = {'body': html.decode('utf8')}
    result = ctxt.eval('run(%s);' % json.dumps(document))

在JScode.js文件的功能如下:

^{pr2}$

在JScode.js我使用的方法有:

document.getElementById();
document.getElementByTagName();
document.evaluate();
document.querySelectorAll();

我该怎么做?在


Tags: 文件对象代码importjsonhtmlevaljs

热门问题