之前做了个小玩意可以搜索自己学校图书馆的图书,那时自己去解析数据非常丑,于是找了个html的解析器解析数据----jsoup。
jsoup是一个非常强大的html解析器。详细用法参考参考文档 http://jsoup.org/apidocs/。
从我们学校的图书馆查询系统用get的方法去查询“小王子”这本书,返回如下html数据:(下面的主要是获得的书籍列表的数据的一小部分而已不是完整的)
对应于我们在电脑上看到的一下这张图的数据
jsoup是一个非常强大的html解析器。详细用法参考参考文档 http://jsoup.org/apidocs/。
从我们学校的图书馆查询系统用get的方法去查询“小王子”这本书,返回如下html数据:(下面的主要是获得的书籍列表的数据的一小部分而已不是完整的)
<h3><span>中文图书</span><a href="item.php?marc_no=0000935570" >1.小王子</a> I565.88/S511:7 </h3>
<p> <span>馆藏复本:2 <br>
可借复本:0</span> (法) 安托万·德·圣埃克苏佩里著 <br />
天津人民出版社 2013 <br />
<img src="../tpl/images/star0.gif" title="总体评分及评价人数"/>(0) <a href="item.php?marc_no=0000935570" class="tooltip" οnmοuseοver="showDetail('0000935570')" οnmοuseοut="hideDetail('0000935570')">馆藏<s></s></a> </p>
<div id="detail0000935570" style="display:none;" >加载中...</div>
</li>
特别要特别注意的是编码的问题,编码与解码不正确的会导致在文乱码等等问题,这里提交的查询的标题的是用了utf-8编码,不然中文就查询出错误了:jsonutil.getjsoncontent(url+URLEncoder.encode(strtitle, "UTF-8"));,这个编码可以在网站的url查看检验出来,除外接收时我也是用了utf-8编码,多次试验没问题,解决了头疼的编码问题:jsonstring=new String(arrayOutputStream.toByteArray(),"UTF-8");
除外上面的 (这种类型的数据是html的unicode编码,不用担心,jsoup的处理会使显示正常:
Element hre=tds2.get(a).child(0); String type=hre.html().toString();
从上面获取的有用数据可以看出:<h3>标签包含了“图书种类”、“书名”、“藏管编号”、“超链接”等数据,查阅了帮助文档后我用以下方法:
Document document = Jsoup.parse(x);//x为获取到的数据 Element trs = document.getElementById("search_book_list");//数据的数据有效标签为search_book_list if (trs != null) //说明查询到数据 { Elements totalTrs = trs.getElementsByClass("book_list_info");//获取到书籍的列表 if (totalTrs.size() > 0)//说明数据有效 for (int i = 0; i < totalTrs.size(); i++) {Elements tds1 = totalTrs.get(i).select("a"); Elements tds2 = totalTrs.get(i).select("h3");//获取到上面说的h3标签的数据 for(int a=0;a<tds2.size();a++) { Element hre=tds2.get(a).child(0); //h3标签里的第一个子标签,就是书籍种类 Element hre1=tds2.get(a).child(1);//第二个子标签,就是超链接 String type=hre.html().toString();// String title=hre1.html().toString(); String hrestr=hre1.attr("href");//获得超链接内容 String body=tds2.get(a).ownText();//获得body对应着书籍的藏馆编号 System.out.println("type----->"+type); System.out.println("title----->"+title); System.out.println("href----->"+hrestr); System.out.println("body----->"+body); } //System.out.println("--------->htmltag"+htmltag); } } else System.out.println("error");//说明查询不到该书籍
好了,看看控制台打印出来的是什么:(省略若干)
type----->中文图书title----->1.爱的传奇:“小王子”和他的玫瑰 href----->item.php?marc_no=0000642349 body----->K835.655.6/S511-3 type----->中文图书 title----->2.小王子 href----->item.php?marc_no=0000692128 body----->I565.88/S511:5 type----->中文图书 title----->3.小王子 href----->item.php?marc_no=1000234748 body----->H329.4/S511.1: 1type----->中文图书 href----->item.php?marc_no=0000642349 body----->K835.655.6/S511-3 type----->中文图书 title----->2.小王子 href----->item.php?marc_no=0000692128 body----->I565.88/S511:5 type----->中文图书 title----->3.小王子 href----->item.php?marc_no=1000234748 body----->H329.4/S511.1: 1type----->中文图书
这已经是我们想要的数据了呀!!
好同理解析剩下我们需要的数据吧:(主要是分析html的数据结构)
好同理解析剩下我们需要的数据吧:(主要是分析html的数据结构)
Elements tds1 = totalTrs.get(i).select("p"); for (int a = 0; a < tds1.size(); a++) { Element hre1 = tds1.get(a); Element hre = tds1.get(a).child(0); String state = hre.ownText(); Node author = hre1.childNode(2);//获取第二个节点 Node nopublicer = hre1.childNode(4);//获取第四个节点 String publicer = nopublicer.toString().substring(0, nopublicer.toString().indexOf("&"));//稍微做点处理 System.out.println("state----->" + state); System.out.println("publicer----->" + publicer); System.out.println("author----->" + author); }
看看控制台输出了什么:
state----->馆藏复本:7 可借复本:6 publicer-----> 南海出版公司 author-----> (法) 阿兰·维康德雷著 state----->馆藏复本:4 可借复本:1 publicer-----> 湖北人民出版社 author-----> (法) 安托万·德·圣埃克苏佩里著 state----->馆藏复本:3 可借复本:2 publicer-----> 社会科学文献出版社 author-----> (法) 安托瓦尼·德·圣-埃克苏佩里著 state----->馆藏复本:2 可借复本:0 publicer-----> 黑龙江少年儿童出版社
同理,获取到书籍的链接时,做一下处理:
Document document = Jsoup.parse(x); Element trs = document.getElementById("tabs2"); if (trs != null) { Elements totalTrs = trs.select("table"); if (totalTrs.size() > 0) for (int i = 0; i < totalTrs.size(); i++) { Elements tds1 = totalTrs.get(0).select("tr"); for (int a = 1; a < tds1.size(); a++) { for(int c=0;c<tds1.get(a).select("td").size();c++) { if(c==2) ; else{ Element hre = tds1.get(a).select("td").get(c); String body = hre.ownText(); System.out.println("number----->" + body); } } }
控制台输出如下:
number----->I565.88/S511:7 number----->A3045638 number----->大学城校区—大学城文学馆 number----->借出-应还日期:2015-03-18 number----->I565.88/S511:7 number----->A3045639 number----->石牌校区—石牌中文文史借阅室 number----->借出-应还日期:2015-03-18
Document document = Jsoup.parse(x);
Element trs = document.getElementById("tabs-1");
if (trs != null) {
Elements totalTrs = trs.getElementsByClass("booklist");
if (totalTrs.size() > 0)
{
for (int i = 0; i < totalTrs.size(); i++) {
if(i==0||i==1||i==2||i==13)
{
Element tds = totalTrs.get(i);
Elements tds1 = totalTrs.get(i).select("dt");
Elements tds2 = totalTrs.get(i).select("dd");
for(int a=0;a<tds1.size();a++)
{
String dt=tds1.get(a).ownText();
String dd=tds2.get(a).ownText();
System.out.println("number----->dt" +dt);
System.out.println("number----->dd" + dd);
}
}else ;
}
}
}
控制台输出如下:
number----->dt题名/责任者: number----->dd:圣埃克苏佩里传/(英)保罗·韦伯斯特(Paul Webster)著 黄喻麟译 number----->dt出版发行项: number----->dd上海:上海人民出版社,2003 number----->dtISBN及定价: number----->dd7-208-04502-X/CNY19.80 number----->dt提要文摘附注: number----->dd本书是风靡全球的童话《小王子》作者的传记,记述了他的童年与成长、进入文学领域的经历,以及作为一名飞行员的意外死亡。
剩下在安卓上调试就好了:
查询到书籍的列表显示出来:
点击进去看到书籍详细内容:
查询到书籍的列表显示出来:
点击进去看到书籍详细内容: