亦有资源网

C++语言基础到进阶学习资源汇总

HTML解析利器Jericho

Jericho HTML Parser是一个开源Java库,允许分析和操作HTML文档,包括服务器端标记,例如ASP、JSP、PHP等语言标记也是可以解析识别的。作为一款小而美的HTML解析工具,它的整个jar包大小只有不到200KB,这使得它不仅可以被用于有需要的服务器端程序,也可以被用于移动设备例如Android App中。目前最新版本为3.4。

1.官方网址

http://jericho.htmlparser.net/docs/index.html


2.下载地址

https://sourceforge.net/projects/jerichohtml/


3.特性

虽然体积不大,但其提供了非常丰富的针对HTML文档的解析功能,相比于其他的HTML解析工具,Jericho有以下特点:

  • 过滤掉任何无法识别的或无效的 HTML,使得解析不规则HTML的容错性好。
  • 可以识别并解析服务器标记语言ASP、JSP、PHP,即使普通HTML中有这些标记,仍然可以正确解析。
  • 一个使用StreamedSource类的基于流的解析选项,它允许使用事件迭代器对大文件进行内存高效处理。
  • 它既不是基于事件的解析器,也不是基于树的解析器,而是使用简单的文本搜索、高效的标签识别和标签位置缓存的组合。
  • 与DOM等基于树的解析器相比,如果只需要解析或修改文档的小部分,那么内存和资源需求会更好。
  • 源文档中每个位置的行号和列号都很容易访问。
  • 可以很容易地定义和注册自定义标记类型,以供解析器识别。
  • 可以从HTML标记中提取所有文本,适用于输入Apache Lucene等文本搜索引擎。
  • 格式化HTML源代码,根据元素在文档元素层次结构中的深度缩进元素。
  • 可以删除所有不必要的空白来压缩HTML源代码。


我们试着解析Jericho官网:
http://jericho.htmlparser.net/docs/index.html,对应源文件:

来看一些简单示例:

public static void main(String[] args) throws MalformedURLException, IOException {
    //jericho官网URL
    String url = "http://jericho.htmlparser.net/docs/index.html";
    Source source = new Source(new URL(url));
    //获取HTML文档encoding属性
    String encoding = source.getEncoding();
    System.out.println(encoding);
    
    //获取HTML文档第一个标签元素
    Element firstElement = source.getFirstElement();
    System.out.println(firstElement);
    
    //获取所有的

标签元素 List elements = source.getAllElements(HTMLElementName.H2); System.out.println(elements); //从HTML标记中提取所有文本 Element firstP = source.getFirstElement(HTMLElementName.P); System.out.println("输出标签中的全部文本内容:" + firstP.getTextExtractor()); //获取文档中第一个DIV标签 StartTag tagDiv = source.getFirstStartTag(HTMLElementName.DIV); //获取属性tagDiv标记的属性 Attributes attributes = tagDiv.getAttributes(); System.out.println("输出标签中的全部属性:" + attributes); //输出属性名、属性值 for (Attribute attr : attributes) { System.out.println("输出标签中的属性键值:" + attr.getName() + " ===> " + attr.getValue()); } //属性替换 Map attributesMap = new HashMap(); attributesMap.put("font-size", "12pt"); attributesMap.put("style", "background-color:red;width:200px;height:200px"); OutputDocument output = new OutputDocument(tagDiv); System.out.println("替换前:" + output.toString()); output.replace(attributes, attributesMap); System.out.println("替换后:" + output.toString()); //缩进,格式化HTML源文件 String text = "
"; Source textSource = new Source(text); String formatText = textSource.getSourceFormatter().toString(); System.out.println("缩进后的内容:"); System.out.println(formatText); //去掉空格 SourceCompactor restoreText = new SourceCompactor(new Segment(new Source(formatText), 0, formatText.length())); System.out.println("HTML压缩后:" + restoreText); }

输出如下:

UTF-8

[

Features

,

Sample Programs

,

Building

,

Alternative HTML Parsers

] 输出标签中的全部文本内容:Jericho HTML Parser is a java library allowing analysis and manipulation of parts of an HTML document, including server-side tags, while reproducing verbatim any unrecognised or invalid HTML. It also provides high-level HTML form manipulation functions. 输出标签中的全部属性: style="float: right; margin-left: 20px" 输出标签中的属性键值:style ===> float: right; margin-left: 20px 替换前:
替换后:
缩进后的内容:
HTML压缩后:
控制面板
您好,欢迎到访网站!
  查看权限
网站分类
最新留言