jsoup 是一款 Java 的HTML 解析器,可直接解析某个URL地址、HTML文本内容。它提供了一套非常省力的API,可通过DOM,CSS以及类似于JQuery的操作方法来取出和操作数据。




File input = new File("/tmp/input.html");
Document doc = Jsoup.parse(input, "UTF-8", "http://example.com/");

Element content = doc.getElementById("content");
Elements links = content.getElementsByTag("a");
for (Element link : links) {
String linkHref = link.attr("href");
String linkText = link.text();

jsoup 1.8.1 发布,极大的性能提升!

soup 1.8.1 显著提升了文本和树序列化的性能;可以选择 HTML 或者 XML 输出;还有大量的功能改进和 bug 修复。此版本现已提供下载。



可以选择 HTML 或者 XML 输出,默认是 HTML
Element.text() 性能改进
Element.html() 性能改进
缩短文件读的时间,同时也改进了文件解析器,提升大概 10% 的速度
添加 Element.cssSelector()
Tightened the scope of what characters are escaped in attributes and textnodes, to align with the spec.
如果禁用了 pretty-print,将不会去除 Element.html() 以外的空格
HTML Cleaner 中允许基础白名单中带有 span 标签,relaxed whitelist 中带有 span 和 div 标签
放松 doctype 验证,可以不指定名称
CSS Selectors 支持 quoted 属性值

Bug 修复

Fixed an issue where <svg><img/></svg> was parsed as <svg><image/></svg>
Fixed an issue where a UTF-8 BOM character was not detected if the HTTP response did not specify a charset, and the HTML body did, leading to the head contents incorrectly being parsed into the body. Changed the behavior so that when the UTF-8 BOM is detected, it will take precedence for determining the charset to decode with.
Fixed an issue in parsing a base URI when loading a URL containing a http-equiv element.
Fixed an issue for Java 1.5 / Android 2.2 compatibility, and verify it doesn't regress.
Fixed an issue that would throw an NPE when trying to set invalid HTML into a title element.
Fixed support for nth-of-type selectors with unknown tags.
Added support for application var wc_add_to_cart_params = {"ajax_url":"\/ajax","i18n_view_cart":"\u67e5\u770b\u8d2d\u7269\u8f66","cart_url":"https:\/\/www.kejianet.cn\/my-cart\/","is_cart":"","cart_redirect_after_add":"no"};