JSoup——用Java解析html网页内容

时间:2024-04-05 13:37:16

当需要从网页上获取信息时,需要解析html页面。筛选指定标签,并获取其值是必不可少的操作,解析html页面这方面的利器,Python有BeautifulSoup,Java一直没有好的工具,之前的HtmlParser非常难用,虽能解析,但不能过滤,只能从头到尾地遍历各个节点,非常不便,而Jsoup是甚至比BeautifulSoup更加方便的工具,Jsoup支持类似于CSS(或jquery)的选择器语法,来实现非常强大和灵活的查找功能。

下面说说其基本用法:

1)Jsoup.parse(html)//建立一个html文本指定的html文档,返回一个Document对象

2)Document.select(filter)//按照过滤器指定条件,筛选满足条件的所有元素(或称为标签),返回一个Elements对象,表示一组Element对象。

3)Elements.first()//返回Elements的第一个Element对象

4)Element.text()//获取一个标签首尾之间的内容,如
String html="<div>我是一个div</div>";
Document doc=Jsoup.parse(html);
doc.getElementsByTag("div").first().text();//输出"我是一个div" 5)Element.attr(attrname),获取标签某个属性的值,如
String html="<div>我是一个div</div>";
Document doc=Jsoup.parse(html);
doc.getElementsByTag("div").first().attr("class")的值为"divStyle"

给出一个综合示例:

public static HashMap<String, Object> getDetails(String html)
{
HashMap<String, Object> details = new HashMap<String, Object>();
     //解析开始
Document doc = Jsoup.parse(html);
     //获取形如<div class="result_info ...">something</div>的第一个div元素,class^=result_info表示class以result_info开头,^是正则表达式的开始
     Element divAll = doc.select("div[class^=result_info]").first();
     
//System.out.println(divAll.text());
Element head = divAll.select("h3").first();//获取div中的h3标签
String tvname = head.select("a").attr("title").trim();//获取h3标签中a标签的title属性值
String year = "";
if (head.select("em").size() > 0)//假如h3标签中存在多个<em></em>标签
{
year = head.select("em").first().text().trim();//只要第一个<em>首尾之间的文本
}
String score = "";
Element scoreSection = divAll.select("p").first();
if (scoreSection != null)
{
Elements es = scoreSection.select("span");//选择span元素
int size = es.size();
for (int i = 0; i < size; i++)
{
Element e = es.get(i);
String content = e.text().trim();
content = content.replace("\u00A0", "");//替换&nbsp;为空格
score += content;
}
}
HashMap<String, String> lstOtherInfo = new HashMap<String, String>();
Elements otherSections = divAll.select("div[class^=result_info_cont]");//获取满足class以result_info_cont开头的所有div元素
int size = otherSections.size();//获取满足条件的div元素的总数是多少
int infoCount = 0;
for (int i = 0; i < size && infoCount < 3; i++)
{
String value = "";
Element item = otherSections.get(i);//获取第i个元素
boolean keyflag = true;
String key = "";
for (int index = 0; index < item.children().size(); index++)//Element.children()用于获取元素的直接子元素
{
Element e = item.child(index);//获取第index个子元素
if (keyflag)
{
key = e.text().trim();
if (key == "简介")
break;
keyflag = false;
}
else
{
if (e.children().size() > 0)
{
for (int b = 0; b < e.children().size(); b++)
{
value += e.child(b).text().trim() + ",";
}
}
else
{
String contents = e.text().trim();
value += contents + ",";
}
}
}
value = value.replaceAll("[" + "," + "]$", "");
lstOtherInfo.put(key, value);
infoCount++;
}
details.put("tv", tvname);
details.put("year", year);
details.put("score", score);
details.put("otherInfo", lstOtherInfo);
return details;
}

参考链接:

http://jsoup.org/apidocs/

https://www.ibm.com/developerworks/cn/java/j-lo-jsouphtml/

http://www.open-open.com/jsoup/selector-syntax.htm