在DIV HTML中遍历或查找元素的最快方法

时间:2011-12-23 09:07:43

标签: java javascript html jsoup jtidy

我正在编写一个实用程序,它应该点击动态页面的URL,检索内容,在各种嵌套div标签中搜索特定的div标签并抓取内容。

主要是,我正在寻找一些Java代码/库。 JavaScript或一些基于JavaScript的库也适用于我。

我入围以下 - > JSoup,Jerry,JTidy(最后更新于2009-12-01)。哪一个是最佳表现?

修改:重新提出问题。添加入围名单。

5 个答案:

答案 0 :(得分:2)

如果你想抓取一个页面并解析它,我建议使用带有jsdom的节点。

安装nodeJS(假设是linux):

sudo apt-get install git
cd ~
git clone git://github.com/joyent/node
cd node
git checkout v0.6
mkdir ~/.local # If it doesn't already exist
./configure --prefix=~/.local
make
make install

还有一个Windows安装程序:http://nodejs.org/dist/v0.6.6/node-v0.6.6.msi

安装jsdom:

$ npm install jsdom

使用您的网址和相关选择器修改此脚本:

var jsdom = require('jsdom');

jsdom.env({
    html: 'url',
    done: function(errors, window) {
        console.log(window.document.getElementById('foo').textContent;
    }
});

答案 1 :(得分:2)

如果您喜欢jQuery的简单语法,可以试试Jerry

Jerry is a jQuery in Java. Jerry is a fast and concise Java Library that simplifies HTML document parsing, traversing and manipulating.
Jerry is designed to change the way that you parse HTML content.

语法似乎很简单。 它应该在最多3行代码中解决您的问题。

答案 2 :(得分:1)

http://jtidy.sourceforge.net/

JTidy非常擅长解析DOM。

答案 3 :(得分:1)

如果你所追求的是一个选择器引擎,那么Sizzle是你最好的选择。它是jQuery使用的引擎。

答案 4 :(得分:0)

为每个div提供唯一ID,并使用document.getElementById(id)

获取