htmlparser的简单使用说明


一、DOM过滤

    1. 根据attribute过滤
        Parser parser = new Parser(htmlString);// or new Parser(url)
        NodeFilter filter = new HasAttributeFilter("class", "sidebar");// 本例为根据class过滤,当然可以标签中的任意attribute
        NodeList nodelist = parser.extractAllNodesThatMatch(filter);

    2. 根据tag过滤
        NodeList nodelist = parser.parse(new TagNameFilter("table"));// 根据table标签过滤
        or
        NodeList  nodelist = parser.parse(new NodeClassFilter(TableTag.class));

    3. 层级and过滤
        // 如查找示例1中class="sidebar"下面的tr标签
        NodeFilter filter = new HasAttributeFilter("class", "sidebar");
        NodeFilter f = new AndFilter(new TagNameFilter("tr"), new HasParentFilter(filter));
        NodeList nodelist = parser.parse(f);
        or
        // 查找所有table下面的tr
        NodeFilter f = new AndFilter(new TagNameFilter("tr"), new HasParentFilter(new TagNameFilter("table")));
        NodeList nl = parser.parse(f);

    4. or满足
        Parser parser = new Parser(url);
        // parser.setEncoding("UTF-8");
        // 过滤得到class为toptext,或者id为sample的内容
        HasAttributeFilter[] f = new HasAttributeFilter[] {
    new HasAttributeFilter("class", "toptext"),
    new HasAttributeFilter("id", "sample")
        };
        NodeFilter nodeFilter = new OrFilter(f);

       // and过滤类似



二、NodeList遍历及子节点
    1. for循环
       for (int i = 0; i < nodeList.size(); i++) {
            Node node = nodeList.elementAt(i);
       }

    2. SimpleNodeIterator
       SimpleNodeIterator iterator = nodeList.elements();
       while(iterator.hasMoreNodes()) {
            Node node = iterator.nextNode();
       }

    3. 子节点
       NodeList pList = node.getChildren();// 获取子节点
    


三、节点判断
    // 判断node是否为head标签
    if (node instanceof org.htmlparser.tags.HeadingTag) {...}

    // 判断node是否为div标签
    if (node instanceof org.htmlparser.tags.Div) {...}

    // 判断node是否为p标签
    if (node instanceof org.htmlparser.tags.ParagraphTag) {...}

    // MORE ...

    // etc:判断当前div是否class为section
    if ("section".equals(((org.htmlparser.tags.Div) node).getAttribute("class"))) {...}


四、内容获取

    // 获取节点源html内容
    node.toHtml()
   
    // 获取节点文本内容,如输入“<td>abc</td>”,输出“abc”
    node.toPlainTextString

你可能感兴趣的:(html,F#)