当需要从网页上获取信息时,需要解析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

最新文章

  1. sublime text3 使用SVN插件
  2. 【QT】自己生成ui加入工程
  3. java字节数组和16进制之间的转换
  4. c#使用MethodInvoker解决跨线程访问控件
  5. NodeJs获取函数名称和函数操作整理
  6. DataView.RowFilter筛选DataTable中的数据
  7. yoga-moblie-res
  8. 严格递增类的dp Codeforces Round #371 (Div. 1) C dp
  9. ubuntu环境下安装Tomcat
  10. jdk1.8新特性 : 接口中可以有普通方法(非静态方法)和静态方法 , 颠覆了之前我的理解 : 接口中只能有共有常量和抽象方法的概念,后面必须要加一句jdk1.7和1..7之前
  11. IntelliJ IDEA安装配置
  12. Educational Codeforces Round 8
  13. 类中函数前、后、参数加const
  14. day16-小数据池
  15. linux运维常见英文报错中文翻译(菜鸟必知)
  16. windows7下docker配置镜像加速
  17. HDU 4640 状态压缩DP 未写完
  18. 【Cf #502 H】The Films(莫队)
  19. 群晖Synology
  20. ASP.NET Web API编程——异常捕获

热门文章

  1. JavaScript事件---事件对象
  2. Git代码管理心得
  3. Microsoft Office下载地址
  4. 【poj1740】 A New Stone Game
  5. POJ2186 Popular Cows
  6. jsp学习(二)
  7. A.2 Main
  8. 清除TFS版本控制信息
  9. javascript “||”、“&amp;&amp;”的灵活运用
  10. 如何通俗地理解 Gradle