Posts Tagged ‘HTML parser’

Jericho HTML Parser 研究学习之例如属性和属性值进行解析

Jericho HTML Parser 研究之属性标签–奥斯软件 

Jericho HTML Parser 的功能确实比较强大,只能一点点不断发掘,今天以baidu法律为例,做了几个小例子,感觉Jericho HTML Parser 的功能确实强大。

/**
  * 显示百度法律文章的具体内容。—奥斯软件
  *
  * @param segments
  */
 private static void displayArticleContent(List<? extends Segment> segments) {
  // Segment segment = segments.get(0);//取出特定标签的信息。
  for (Segment segment : segments) {
   List<Element> linkElements = segment.getAllElements();
   for (Element linkElement : linkElements) {
    String href = linkElement.getAttributeValue(”class”);
    if (href == null || !href.equalsIgnoreCase(”f”))
     continue;
    // System.out.println(linkElement.getDebugInfo());
    // 获得纯文本法律内容
    // String
    // label=linkElement.getContent().getTextExtractor().toString();
    // 获得带有格式的法律内容
    String label = linkElement.getContent().toString();
    System.out.println(label);
   }
  }
  System.out.println(”n*******************************************************************************n”);
 }

用Jericho HTML Parser开源软件在解析的过程中,如何对source的URL进行批量处理:

利用下面的方法可以循环处理Href,从而利用Jericho HTML Parser不断展现其强大的功能。

private static void proceeParseHTML(List arrHref){
  try{
  for(int i=0;i< arrHref.size();i++){
  String sourceUrlString = arrHref.get(i).toString();
  
  if (sourceUrlString.indexOf(’:') == -1)
   sourceUrlString = “file:” + sourceUrlString;
  // MicrosoftTagTypes.register();
  // MasonTagTypes.register();
  Source source = new Source(new URL(sourceUrlString));
  displayArticleContent(source.getAllElements(HTMLElementName.TD));
  displayArticleTitle(source.getAllElements(HTMLElementName.TITLE));
  //displayArticleHREF(source.getAllElements(HTMLElementName.A));
  }
  }catch(Exception e){
   System.out.println(e.getMessage());   
  }  
 }

请留评论

利用jericho和htmlparser对网页进行解析—以读取酒立方的文章为例

研究开源项目jericho,并利用jericho对html进行解析的一些心得体会。

下面只是读取其中文章主题内容的一个例子,展现了jericho读取文章的基本功能。

优点:操作简单,类库强大

缺点:速度不尽人意

下一步:继续研究jericho的代码,并计划对其扩展,提高性能或者结合其他html解析工具对性能进行处理。基本思路:可以从网页下载方面下手。

希望有研究网页爬虫和html解析方面的朋友多提意见。

package cn.com.ossoftware;

import java.net.URL;
import java.util.List;

import net.htmlparser.jericho.Element;
import net.htmlparser.jericho.HTMLElementName;
import net.htmlparser.jericho.MasonTagTypes;
import net.htmlparser.jericho.MicrosoftTagTypes;
import net.htmlparser.jericho.PHPTagTypes;
import net.htmlparser.jericho.Source;
/**
 * 解析www.flzj.com中的文章。
 * 1,截取所有的文章,然后分析按分类进行整理导入到新的网站中。
 * 2,对内容进行分析整理。
 * @author peterwang
 *
 */
public class ExtractTextFromJiulifang {public static void main(String[] args) throws Exception {
 
 String sourceUrlString=”http://www.spirits.jiulifang.com/brandy-news/17601.html”;//文章的链接,根据具体情况处理。
 
 //String sourceUrlString=”http://www.flzj.com/html/flxw/quexun/23050.html“;//对法律专家网站文章进行解析
 if (args.length==0)
   System.err.println(”Using default argument of “”+sourceUrlString+’”‘);
 else
  sourceUrlString=args[0];
 if (sourceUrlString.indexOf(’:')==-1) sourceUrlString=”file:”+sourceUrlString;
 MicrosoftTagTypes.register();
 PHPTagTypes.register();
 PHPTagTypes.PHP_SHORT.deregister(); // remove PHP short tags for this example otherwise they override processing instructions
 MasonTagTypes.register();
 Source source=new Source(new URL(sourceUrlString));

 // Call fullSequentialParse manually as most of the source will be parsed.
 source.fullSequentialParse();
 

 System.out.println(”nLinks to other documents:”);
 //List<Element> linkElements=source.getAllElements(HTMLElementName.A);
 
 List<Element> linkElements=source.getAllElements(HTMLElementName.DIV);//获得法律专家网站的div标签,后者是对酒立方文章的div标签进行过滤。
 //过滤特殊的片断–peter 获得法律专家的文章正文内容
 for (Element linkElement : linkElements) {
  String href=linkElement.getAttributeValue(”class”);   
  if (href == null || !href.equalsIgnoreCase(”area_article”)) continue;//具体判断是哪个div,从而对法律专家网站和酒立方网站的文章进行处理
  //System.out.println(”peter__________class=”+” <”+href+’>’);
  // A element can contain other tags so need to extract the text from it:
  /*获得文章文字内容*/
  //String label=linkElement.getContent().getTextExtractor().toString();//法律专家和酒立方文章文字进行输出。
  //System.out.println(label+” <”+href+’>’);
  //element的内容就是html源码。
  String label=linkElement.getContent().toString();//html内容输出
  System.out.println(label+” <”+href+’>’);  
  //System.out.println(linkElement.getSource().clearCache());
    
 }
}

}

更多研究jericho的文章,Jericho HTML Parser 研究学习 — http://www.wanghongbo.com/archives/320 

输出结果如下:

正确品尝白兰地烈酒的三步骤详解 【文章来源】:新华网 【日期】:2010-01-05       白兰地是以水果为原料,经过发酵、蒸馏、贮藏陈酿而成的蒸馏酒。通常讲白兰地是以葡萄为原料酿制而成的,而以其他水果为原料酿成的白兰地,应冠以原料水果的名称,如苹果白兰地、樱桃白兰地等。     白兰地作为世界四大蒸馏名酒之一,是以法国白兰地品质为代表的。目前世界上产量最大、声誉最高的白兰地是法国科涅克。科涅克是法国夏朗德省的一座古镇,这里有大片的葡萄园,大大小小生产科涅克的工厂比比皆是,年平均产量为500万加仑(纯酒精)科涅克。目前,我国国产白兰地的年产量为2万多吨。     要真正领略白兰地的韵味,就要有正确的品尝方法。白兰地的品尝大致可以分为三步,首先是看酒的清度与颜色。上乘白兰地为琥珀色、晶莹光灿、庄重而不娇艳。第二步是闻香,白兰地除具有葡萄原料的果香外,更主要的需具有常年贮存于橡木桶中,酒与橡木结合而产生的香气,这种香气虽经加水冲稀仍能保持原来的香气特点,用香料配成的酒香气浮,不醇和,并且无贮存的橡木香,冲稀即变味。优质的白兰地味感应具有贮存橡木香,它不仅进口柔软,入腹发热,还有诱人的水果鲜香味和幽雅醇厚的陈酿香味,味韵协调,回味绵延。通过闻香,白兰地质量的好坏可以确定60%。第三步也是最关键的一步,就是入口。第一口用舌尖抿一小滴,让其沿舌尖蔓延整个舌头,再进入喉咙。第二口可以稍多些,进一步领略温柔醇香的独特感觉。     在酒类货架上,到处可见各种牌号的法国白兰地、中国产白兰地及各种中外合资企业生产的白兰地。消费者在选购白兰地时,首先能看到的是产品的外在品质。好的白兰地产品,首先在视觉上就会给人以美的享受:澄清透亮、晶莹光灿,颜色一般为金黄色或者赤金黄色,庄重而不娇艳。白兰地的颜色和色调,在某种程度上反映白兰地贮藏时间的长短和质量的好坏,如果酒液呈暗红或瓦灰色,说明质量较差。     白兰地产品标签上标注的内容,也为消费者选择适合自己的产品提供了重要的信息。一是产品的名称,如果名称为其他水果白兰地(或X.O、VSOP等),则这个产品就不是以葡萄为原料酿成的。二是产品配料,在产品的标签上有配料表的内容,消费者在购买时要看清配料,确认原料是葡萄或其他水果。三是产品等级,不同等级的白兰地品质不同,其价格差异也很悬殊,消费者应该根据自己的个人喜好以及消费水平,参考上面对于白兰地分级的介绍,选择适合的产品。     另外,选购白兰地时,认清白兰地牌号,尽量选择一些名牌和大型企业的产品,这些企业具备较成熟的工艺和完备的技术、设备,质量更有保证。   相关文章: 网友评论 查看全部评论 尚未有评论 我来说两句 评分 -5 -3 -1 - +1 +3 +5 内容 昵称 验证 提交评论 <area_article>

 

Comments Off

Jericho HTML Parser 研究学习

目前在研究如何自动从网络上抓取(获得)自己想要的信息,同时对信息按照自己的需要进行分类,归档处理。

一切还想以前,看看有没有开源的东东可以借鉴,站在别人的基础上工作速度会快些,虽然并不都会取得好的效果。

看到挺多人都在关注Jericho HTML Parser,应该是个不错的家伙,拿过来测试了一下,基本功能都可以满足,索性就研究一下:

Jericho HTML Parser 简介:

Jericho HTML Parser是一个简单而功能强大的Java HTML解析器库,可以分析和处理HTML文档的一部分,包括一些通用的服务器端标签,同时也可以重新生成无法识别的或无效的HTML。它也提供了一个有用的HTML表单分析器。

作者的主页在:http://jericho.htmlparser.net/docs/index.html

相关项目信息在:http://jerichohtml.sourceforge.net/

附录原作者的介绍:—-Jericho HTML Parser 对作者表示感谢

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.

It is an open source library released under both the Eclipse Public License (EPL) and GNU Lesser General Public License (LGPL). You are therefore free to use it in commercial applications subject to the terms detailed in either one of these licence documents.

The javadocs provide comprehensive documentation of the entire API, as well as being a very useful reference on aspects of HTML and XML in general.

Visit the SourceForge.net project page at http://sourceforge.net/projects/jerichohtml/ for downloads and support.

You can also rate the project highly at http://freshmeat.net/projects/jerichohtml/

Release notes for each version can be found in a file called release.txt in the project root directory.

下面会对Jericho HTML Parser 的例子进行一些解释和实际操作,同时增加自己的一些扩展和应用。

The following sample programs are available:下面试一下简单的实例程序,源代码可以自己点击下载:

DisplayAllElements.java Demonstrates the behaviour of the library when retrieving all elements from a document containing a mix of normal HTML, different types of server tags, and badly formatted HTML.演示获得所有的页面元素,包括一些服务器端的标签。
FindSpecificTags.java Demonstrates how to search for tags with a specified name, in a specified namespace, or special tags such as document type declarations, XML declarations, XML processing instructions, common server tags, PHP tags, Mason tags, and HTML comments.
ExtractText.java Demonstrates the use of the TextExtractor class that extracts all of the text from a document, as well as the title, description, keywords and links.
RenderToText.java Demonstrates the use of the Renderer class that performs a simple text rendering of HTML markup, similar to the way Mozilla Thunderbird and other email clients provide an automatic conversion of HTML content to text in their alternative MIME encoding of emails.获得HTML页面的文本信息-便于全文检索的应用,像Apache Lucene等。
HTMLSanitiser.java Demonstrates how to sanitise HTML containing unwanted or invalid tags into clean HTML. The unit test class for this functionality is available here.对HTML中不想要的或者无效的标签进行清理。对html标签进行标准化处理–参照HTML标准进行处理,遗漏的地方可以自己扩展。——-可以扩展为html检测器和代码自动更正引擎之类的东东。
StreamedSourceCopy.java Demonstrates the use of the StreamedSource class by iterating through the parsed segments of a source document and creating an exact copy of it.

复制源代码。

FormControlDisplayCharacteristics.java Demonstrates setting the display characteristics of individual form controls. This allows a control to be disabled, removed, or replaced with a plain text representation of its value (display value). The new document is written to a file called NewForm.html
FormFieldCSVOutput.java Demonstrates the use of the FormFields.getColumnValues(Map) method to store form data in a .CSV file, automatically creating separate columns for fields that can contain multiple values (such as checkboxes). The output is written to a file called FormData.csv
FormFieldList.java Demonstrates the use of the Segment.findFormFields() method to list all form fields and their associated controls in a document.
FormFieldSetValues.java Demonstrates setting the valunnecessary ues of form controls, which is best done via the FormFields object. The new document is written to a file called NewForm.html
FormatSource.java Demonstrates the use of the SourceFormatter class that formats HTML source by laying out each non-inline-level element on a new line with an appropriate indent. Also known as a “source beautifier”. (Click here for an online demonstration)
CompactSource.java Demonstrates the use of the SourceCompactor class that compacts HTML source by removing all white space.
Encoding.java Demonstrates the use of the EncodingDetector class and how to determine the encoding of a source document.
SplitLongLines.java Demonstrates how to reformat a document so that lines exceeding a certain number of characters are split into multiple lines.对长行进行折行处理。
ConvertStyleSheets.java Demonstrates how to detect all external style sheets and place them inline into the document.

评论 (2)