whmwg 发表于 2013-2-7 20:48:25

记录一下jsoup解析html(2)

首先看一下org.jsoup.nodes所有类的继承关系,其中比较常用的就是Element和Document两个类
http://dl.iteye.com/upload/attachment/0078/1395/f7d60305-5cdd-3792-937f-4c6e24cd4d18.jpg


现在来看一下“使用DOM或CSS选择器来查找、取出数据”的功能
(1)DOM方式
使用DOM即直接操作html dom对象来查找、取出数据,和javascript中操作html dom类似,只不过jsoup有它自己的方法,看官方文档可以看到,截取Element类中的部分方法:
http://dl.iteye.com/upload/attachment/0078/1397/ebddc8bc-9331-3e11-b09e-eed5fac2c4d6.jpg
不多说了,直接上一段采用DOM方式解析的代码,其中baiduHtml.txt为百度首页html的文本文件,目的是查找id为content节点包含的所有<a>链接:
public static void main(String[] args) throws IOException {File file = new File("baiduHtml.txt");Document document = Jsoup.parse(file, "UTF-8");Element contentEle = document.getElementById("content");Elements aEles = contentEle.getElementsByTag("a");System.out.println("size:"+aEles.size());for (Element e : aEles) {String href = e.attr("href");String text = e.text();System.out.println(text + "---" + href);}}
输出结果如下:
size:17搜索设置---http://www.baidu.com/gaoji/preferences.html登录---https://passport.baidu.com/v2/?login&tpl=mn&u=http%3A%2F%2Fwww.baidu.com%2F注册---https://passport.baidu.com/v2/?reg&regType=1&tpl=mn&u=http%3A%2F%2Fwww.baidu.com%2F新 闻---http://news.baidu.com贴 吧---http://tieba.baidu.com知 道---http://zhidao.baidu.com音 乐---http://music.baidu.com图 片---http://image.baidu.com视 频---http://video.baidu.com地 图---http://map.baidu.com手写---#拼音---#关闭---#百科---http://baike.baidu.com文库---http://wenku.baidu.comhao123---http://www.hao123.com更多>>---http://www.baidu.com/more/

DOM方式比较符合经常用javascript操作html dom的习惯,但是jsoup更为强大的是还具有类似jquery的CSS选择器方式(据网上说的,反正jquery我也没用过)

(2)CSS选择器方式
什么都不说,先来一段采用CSS选择器方式解析的代码来简单的认识一下CSS选择器,目的是获取所有以http://开头的<a>链接,最后输出这些<a>链接的href属性
public static void main(String[] args) throws IOException {File file = new File("baiduHtml.txt");Document doc = Jsoup.parse(file, "UTF-8");Elements elements = doc.select("a");System.out.println("size:" + elements.size());for (Element e : elements) {System.out.println(e.attr("href"));}}
输出结果如下:
size:19http://www.baidu.com/gaoji/preferences.htmlhttp://news.baidu.comhttp://tieba.baidu.comhttp://zhidao.baidu.comhttp://music.baidu.comhttp://image.baidu.comhttp://video.baidu.comhttp://map.baidu.comhttp://baike.baidu.comhttp://wenku.baidu.comhttp://www.hao123.comhttp://www.baidu.com/more/http://www.baidu.com/cache/sethelp/index.htmlhttp://www.baidu.com/search/baidukuaijie_mp.htmlhttp://e.baidu.com/?refer=888http://top.baidu.comhttp://home.baidu.comhttp://ir.baidu.comhttp://www.miibeian.gov.cn
再来一些简单的小例子说明:
Elements links = doc.select("a"); // 具有href 属性的a链接   Elements pngs = doc.select("img"); //所有引用png图片的元素  Element masthead =doc.select("div.masthead").first(); //找出定义了class=masthead 的div元素的第一个Elements resultLinks = doc.select("h3.r>a"); //direct a after h3
关于其它的选择器语法,可以查看官方文档Selector类的介绍,截图如下,若想看中文介绍-->点击看
http://dl.iteye.com/upload/attachment/0078/1406/9b6ec3e2-b398-3ea8-904c-a559d446d872.jpg

http://dl.iteye.com/upload/attachment/0078/1408/06ef8896-bff7-37a5-a2af-af7f2747cd51.jpg

http://dl.iteye.com/upload/attachment/0078/1410/680c6e23-fda2-369e-8936-1631b0095f51.jpg
页: [1]
查看完整版本: 记录一下jsoup解析html(2)