设为首页收藏本站
开启辅助访问
切换到宽版

创星网络[分享知识 传递快乐]

 找回密码
 立即注册

QQ登录

只需一步,快速开始

用新浪微博登录

只需一步,快速搞定

搜索
查看: 4372|回复: 0
打印 上一主题 下一主题

jsoup 1.6.2发布 最棒的Java HTML解析器

[复制链接]

我玩的应用:

跳转到指定楼层
楼主
发表于 2012-10-18 00:48:25 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

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

jsoup 1.6.2 发布了,改版包含很多的 bug 修复,松散的 XML 解析模式,功能调整以及内存的改进。

主要改进内容包括:

- Added a simplified XML parsing mode, which can usefully parse valid and invalid XML, but does not enforce any HTML document structure or special tag behaviour.
- Added the optional ability to track errors when tokenising and parsing.
- Added Jsoup.connect.cookies(Map) method, to set multiple cookies at once, possibly from a prior request.
- Added Element.textNodes() and Element.dataNodes(), to easily access an element's children text nodes and data nodes.
- Added an example program that demonstrates how to format HTML as plain-text, and the use of the NodeVisitor interface.
- Added Node.traverse() and Elements.traverse() methods, to iterate through a node's descendants.
- Updated Jsoup.connect() so that when requests made as POSTs are redirected, the redirect is followed as a GET.
- Updated the Cleaner and whitelists to optionally preserve related links in elements, instead of converting them to absolute links.
- Updated the Cleaner to support custom allowed protocols such as "cid:" and "data:".
- Updated handling of base href tags, to act on only the first one seen when parsing, to align with modern browsers.
- Updated Node.setBaseUri(), to recursively set on all the node's descendants.
Bug fixes:
- Fixed an issue where all HTML parse errors where being tracked as new objects, creating high memory pressure on low-memory devices.
- Fixed handling of null characters within comments.
- Tweaked escaped entity detection in attributes to not treat &entity_... as an entity form.
- Fixed doctype tokeniser to allow whitespace between name and public identifier.
- Fixed issue where comments within a table tag would be duplicate-fostered into body.
- Fixed an issue where a spurious byte-order-mark at the start of a document would cause the parser to miss head contents.
- Fixed an issue where content after a frameset could cause a NPE crash. Now correctly implements spec and ignores the trailing content.
- Tweaked whitespace checks to align with HTML spec.
- Tweaked HTML output of closing script and style tags to not add an extraneous newline when pretty-printing.
- Substantially reduced default memory allocation within Node.outerHtml, to reduce memory pressure when serialising smaller DOMs.

详情请看官方发行说明:

http://jsoup.org/news/release-1.6.2

jsoup的主要功能如下:


  1. 从一个URL,文件或字符串中解析HTML;
  2. 使用DOM或CSS选择器来查找、取出数据;
  3. 可操作HTML元素、属性、文本;
复制代码

jsoup是基于MIT协议发布的,可放心使用于商业项目。

示例代码:

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

  3. Element content = doc.getElementById("content");  
  4. Elements links = content.getElementsByTag("a");  
  5. for (Element link : links) {  
  6.   String linkHref = link.attr("href");  
  7.   String linkText = link.text();  
  8. }
复制代码
下载地址:http://jsoup.org/download

from:http://developer.51cto.com/art/201203/326186.htm
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 转播转播 分享分享 分享淘帖
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|创星网络 ( 苏ICP备11027519号|网站地图  

GMT+8, 2024-9-22 07:12 , Processed in 0.092179 second(s), 23 queries .

Powered by Discuz! X3

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表