Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/458.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
支持javascript的Python html解析器~ 文件。写(“”); 写下(“f***js!”); 文件。写(“”); 写下(“f***js!”);_Javascript_Python_Lxml - Fatal编程技术网

支持javascript的Python html解析器~ 文件。写(“”); 写下(“f***js!”); 文件。写(“”); 写下(“f***js!”);

支持javascript的Python html解析器~ 文件。写(“”); 写下(“f***js!”); 文件。写(“”); 写下(“f***js!”);,javascript,python,lxml,Javascript,Python,Lxml,我想使用xpath捕获上面html页面中的所有标签对象 <html> <head> <script type="text/javascript"> document.write('<a href="http://www.google.com">f*** js</a>'); document.write("f*** js!"); </script> </head> <body&

我想使用xpath捕获上面html页面中的所有标签对象

<html>
<head>
    <script type="text/javascript">
    document.write('<a href="http://www.google.com">f*** js</a>');
    document.write("f*** js!");
    </script>
</head>
<body>
    <script type="text/javascript">
    document.write('<a href="http://www.google.com">f*** js</a>');
    document.write("f*** js!");
    </script>
<div><a href="http://www.google.com">f*** js</a></div>
</body>
</html>
[1]中的
:将lxml.html导入为H
在[2]中:f=open(“test.html”,“r”)
在[3]中:c=f.read()
在[4]中:doc=H.document\u fromstring(c)
[5]中的doc.xpath('//a')
输出[5]:[]
在[6]中:a=doc.xpath('//a')[0]
在[7]中:a.getparent()
出[7]:
我只得到一个不由js生成的 但是FirefoxXPath检查器可以找到所有标签

怎么做???thx~

In [1]: import lxml.html as H

In [2]: f = open("test.html","r")

In [3]: c = f.read()

In [4]: doc = H.document_fromstring(c)

In [5]: doc.xpath('//a')
Out[5]: [<Element a at a01d17c>]

In [6]: a = doc.xpath('//a')[0]

In [7]: a.getparent()
Out[7]: <Element div at a01d41c>

()上的函数{
a、 innerHTML=“鼠标移动我”
}
函数输出(){
a、 innerHTML=“”
}
鼠标点击我

在Java中有。我不知道任何Python的Javascript感知HTML解析器。

对Python中的Javascript感知解析器一无所知,但您可以使用它来完成这项工作。这个主意不是我的,所以我把它留给你

这其实很酷,因为你可以优化你的解析器,有选择地选择需要解析(和执行)的指令。

在谷歌搜索“javascript独立运行时”,我发现:基于“Gecko的JavaScript引擎库”


听起来不错!我还没有测试过,但这似乎可以让你运行你在页面中找到的javascript代码。我不知道这会有多棘手,但是..

删除了亵渎,因为它没有任何作用..你必须在解析HTML之前解析和解释js。你看到了吗?你的javascript,就像给定的一样,没有意义-哟你正在将链接写入文档的头部?从lxml的角度来看,document.write中的任何内容都是字符串常量,不需要解析。顺便说一句,XML文档中不允许使用document.write()。你必须使用DOM API。我想标题应该是“支持javascript的Python html解析器””“太好了!从你链接的同一个问题来看,似乎也是值得考虑的…不太。。。它只是语言绑定,但没有DOM API。大多数现实世界的javascript仍然无法在其中工作。当您添加所有需要的部件时,您将拥有。。。浏览器。或者,我知道的最接近的事情是。
<html>
<head>
</head>
<body>
<script language="javascript">
function over(){
a.innerHTML="mouse me"
}
function out(){
a.innerHTML="<a href='http://www.google.com'>google</a>"
}
</script>
<body><li id="a"onmouseover="over()" onmouseout="out()">mouse me</li>
</body>
</html>