利用HtmlParser解析网页内容

一,htmpparser介绍

  htmlparser是一个功能比较强大的网页解析工具,主要用于 html 网页的转换(Transformation) 以及网页内容的抽取 (Extraction)。

二,使用与示例

  1,提取网页某类型标签.这里,html的标签对应于一个标签类,如<img>标签对应于ImageTag.下面以提取<img>标签为例,输出网页图片地址:

        //读取文件内容
        String html = IOUtils.toString(new FileInputStream(localFile), "UTF-8");
        
        //创建html解析器
        Parser parser = new Parser();
     //设置解析的网页内容 parser.setInputHTML(html); NodeList imageTags
= parser.parse(new NodeClassFilter(ImageTag.class)); for(int i=0; i<imageTags.size(); i++){ ImageTag it = (ImageTag) imageTags.elementAt(i); String imageUrl = it.getImageURL(); System.out.println(imageUrl); }

  2,提取特定标签.当要提取出某个属性值为xx的标签时,需要自定义过滤器来设定规则,获得相对应的标签。

        String html = IOUtils.toString(new FileInputStream(localFile), "UTF-8");
        
        //创建html解析器
        Parser parser = new Parser();
        parser.setInputHTML(html);
        
        //自定义过滤器来拿到指定名字的标签<meta name="keywords">
        NodeList metaTags = parser.parse(
                new NodeFilter() {
                    
                    @Override
                    public boolean accept(Node node) {
                        if(node instanceof MetaTag){
                            MetaTag mt = (MetaTag) node;
                            
                            if(mt.getMetaTagName() != null && mt.getMetaTagName().equals("keywords")){
                                return true;
                            }
                        }
                        return false;
                    }
                }
                );
        
        for(int i=0; i<metaTags.size(); i++){
            MetaTag mt = (MetaTag) metaTags.elementAt(i);
            System.out.println("meta keyword value : " + mt.getMetaContent());
        }

  3,通过以上的例子,熟悉了htmlparser的简单实用后,我们可以封装出一个方法,传入参数html内容,标签类型,标签属性名,标签属性值四个参数,就可返回特定标签列表。当然可以省略后两个参数,获得一系列某类标签。

    /**
     * 提取具有某个属性的标签列表
     * @param html  被提取的html文本
     * @param tagType  标签类型
     * @param attributeName  标签属性名称
     * @param attributeValue  该属性的值
     * @return
     */
    public static <T extends TagNode> List<T> parseTags(String html,final Class<T> tagType,final String attributeName,final String attributeValue){
        
        try {
            Parser parser = new Parser();
            parser.setInputHTML(html);
            
            //自定义过滤器来拿到指定名字的标签
            NodeList nodeList = parser.parse(
                    new NodeFilter() {
                        
                        @Override
                        public boolean accept(Node node) {
                            //如果是同类型的标签
                            if(node.getClass() == tagType){
                                T t = (T) node;
                                
                                //只过滤该类型的标签
                                if(attributeName == null){
                                    return true;
                                }
                                
                                String attrValue = t.getAttribute(attributeName);
                                //过滤掉特定属性名字的标签
                                if(attrValue != null && attrValue.equals(attributeValue)){
                                    return true;
                                }
                            }
                            return false;
                        }
                    }
                    );
            
            List<T> tags = new ArrayList<T>();
            for(int i=0; i<nodeList.size(); i++){
                T t = (T) nodeList.elementAt(i);
                tags.add(t);
            }
            return tags;
            
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        return null;
    }
View Code

最后,这里只是简单介绍htmlparser2使用方法,详细情况请参看其官方文档。

posted @ 2013-10-11 15:13  剑志  阅读(1002)  评论(0编辑  收藏  举报