如何提取由脚本使用python请求生成的网页数据



我有一个html代码,它返回PI 的值

<!DOCTYPE html>
<html>
<body>
<p>This example calls a function which returns the value of PI:</p>
<p id="demo"></p>
<script>
function myFunction() {
return Math.PI;
}
document.getElementById("demo").innerHTML = myFunction();
</script>
</body>
</html>

当我使用python请求lib时,我只得到与上面完全相同的源代码,但没有得到Math.PI.的值

我只想提取PI的值,我该如何使用python?

您可以使用Piotr Dabkowski的js2py库。

只需使用解析器或正则表达式即可获得所需的函数,然后:

#pip install js2py
import js2py
js = """
function myFunction() {
return Math.PI;
}
"""
myFunction = js2py.eval_js(js)
myFunction()
#Returns
3.141592653589793

在浏览器中运行html代码后,您需要查看该代码
在这种情况下,使用无头浏览器即可。它们有很多可供选择,比如活性汤

最新更新