支持JavaScript的Python 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>
我想用xpath来抓取上面这个网页中的所有标签对象...
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>
我只抓到了一个,那个是没有通过js生成的~但是火狐的xpath检查工具可以找到所有标签!?
我该怎么做呢??? 谢谢~!
<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>
3 个回答
0
我在谷歌上搜索“javascript 独立运行环境”,发现了 jslibs:这是一个“独立的 JavaScript 开发运行环境,可以把 JavaScript 当作通用脚本语言来使用”,它是基于“SpiderMonkey库,这个库是 Gecko 的 JavaScript 引擎”。
听起来不错!我还没测试过,但看起来这个工具可以让你运行网页上找到的 JavaScript 代码。不过,我不知道这会有多复杂……
0
在Java中,有一个叫做Cobra的工具。不过我不知道Python里有没有能识别JavaScript的HTML解析器。