使用 Jerry 来解析 HTML 文档,类似 jQuery,,Jerry 是 Jodd
分享于 点击 46066 次 点评:42
使用 Jerry 来解析 HTML 文档,类似 jQuery,,Jerry 是 Jodd
Jerry 是 Jodd工具包中的一个HTML解析工具。它有点像是用Java实现的jQuery。Jerry是一个高效简洁的Java库,用来简化HTML的解析,遍历和操作。
[Java]代码
public class AllMusicNewReleases { public static void main(String[] args) throws IOException { // download the page super-efficiently File file = new File(SystemUtil.getTempDir(), "allmusic.html"); NetUtil.downloadFile("http://allmusic.com", file); // create Jerry, i.e. document context Jerry doc = Jerry.jerry(FileUtil.readString(file)); // parse doc.$("div#new_releases div.list_item").each(new JerryFunction() { public boolean onNode(Jerry $this, int index) { System.out.println("-----"); System.out.println($this.$("div.album_title").text()); System.out.println($this.$("div.album_artist").text().trim()); return true; } }); }}
修改Google页面
public class ChangeGooglePage { public static void main(String[] args) throws IOException { // download the page super-efficiently File file = new File(SystemUtil.getTempDir(), "google.html"); NetUtil.downloadFile("http://google.com", file); // create Jerry, i.e. document context Jerry doc = Jerry.jerry(FileUtil.readString(file)); // remove div for toolbar doc.$("div#mngb").detach(); // replace logo with html content doc.$("div#lga").html("<b>Google</b>"); // produce clean html... String newHtml = doc.html(); // ...and save it to file system FileUtil.writeString( new File(SystemUtil.getTempDir(), "google2.html"), newHtml); }}
用户点评