我使用Jsoup来解析html文件,并从元素中提取所有可见文本。问题是javascript变量中的一些html位显然被忽略了。什么是最好的解决方案来让这些比特出来?
示例:
<!DOCTYPE html>
<html>
<head>
<script>
var html = "<span>some text</span>";
</script>
</head>
<body>
<p>text</p>
</body>
</html>在本例中,Jsoup只从p标记中提取文本,这是它应该做的事情。如何从var html span中提取文本?该解决方案必须应用于数千个不同的页面,所以我不能依赖于具有相同名称的javascript变量。
发布于 2013-07-29 19:42:34
您可以使用Jsoup将所有的<script>-tags解析为DataNode-objects。
DataNode一个数据节点,用于样式、脚本标记等内容,其中的内容不应显示在文本中()。
Elements scriptTags = doc.getElementsByTag("script");这将为您提供标记<script>的所有元素。
然后,可以使用getWholeData()-method提取节点。
//获取该节点的数据内容。String getWholeData()
for (Element tag : scriptTags){
for (DataNode node : tag.dataNodes()) {
System.out.println(node.getWholeData());
}
}Jsoup API - DataNode
发布于 2013-11-02 12:16:53
我不太确定答案,但我在here之前看到过类似的情况。
您可能可以使用Jsoup和手动解析来根据该答案获得文本。
我只是针对您的特定情况修改了这段代码:
Document doc = ...
Element script = doc.select("script").first(); // Get the script part
Pattern p = Pattern.compile("(?is)html = \"(.+?)\""); // Regex for the value of the html
Matcher m = p.matcher(script.html()); // you have to use html here and NOT text! Text will drop the 'html' part
while( m.find() )
{
System.out.println(m.group()); // the whole html text
System.out.println(m.group(1)); // value only
}希望对您有所帮助。
https://stackoverflow.com/questions/17922129
复制相似问题