HtmlParser 简单测试实例

package  test;

import  java.net.URL;  
import  org.htmlparser. * ;
import  org.htmlparser.beans.LinkBean;
import  org.htmlparser.filters.NodeClassFilter;
import  org.htmlparser.filters.OrFilter;
import  org.htmlparser.filters.TagNameFilter;
import  org.htmlparser.tags. * ;
import  org.htmlparser.util.NodeIterator;
import  org.htmlparser.util.NodeList;
import  org.htmlparser.util.ParserException;
import  org.htmlparser.visitors.HtmlPage;
import  org.htmlparser.visitors.NodeVisitor;
import  org.htmlparser.visitors.ObjectFindingVisitor; 

ExpandedBlockStart.gifContractedBlock.gif
public   class  ParserTestCase 
    
ExpandedSubBlockStart.gifContractedSubBlock.gif     
public static void main(String[]args){
         ParserTestCase testCase 
= new ParserTestCase("MyTest");
         testCase.testTable(); 
// 替换成需要测试的方法
     }

    
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public ParserTestCase(String name) 
    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//*
     * 测试ObjectFindVisitor的用法
     
*/

ExpandedSubBlockStart.gifContractedSubBlock.gif    
public void testImageVisitor() {
ExpandedSubBlockStart.gifContractedSubBlock.gif        
try {
            ImageTag imgLink;
            ObjectFindingVisitor visitor 
= new ObjectFindingVisitor(
                    ImageTag.
class);
            Parser parser 
= new Parser();
            parser.setURL(
"http://www.google.com");
            parser.setEncoding(parser.getEncoding());
            parser.visitAllNodesWith(visitor);
            Node[] nodes 
= visitor.getTags();
ExpandedSubBlockStart.gifContractedSubBlock.gif            
for (int i = 0; i < nodes.length; i++{
                imgLink 
= (ImageTag) nodes[i];
               System.out.println(
"testImageVisitor() ImageURL = "
                        
+ imgLink.getImageURL());
               System.out.println(
"testImageVisitor() ImageLocation = "
                        
+ imgLink.extractImageLocn());
               System.out.println(
"testImageVisitor() SRC = "
                        
+ imgLink.getAttribute("SRC"));
            }

        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
catch (Exception e) {
            e.printStackTrace();
        }

    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//*
     * 测试TagNameFilter用法
     
*/

ExpandedSubBlockStart.gifContractedSubBlock.gif    
public void testNodeFilter() {
ExpandedSubBlockStart.gifContractedSubBlock.gif        
try {
            NodeFilter filter 
= new TagNameFilter("IMG");
            Parser parser 
= new Parser();
            parser.setURL(
"http://www.google.com");
            parser.setEncoding(parser.getEncoding());
            NodeList list 
= parser.extractAllNodesThatMatch(filter);
ExpandedSubBlockStart.gifContractedSubBlock.gif            
for (int i = 0; i < list.size(); i++{
               System.out.println(
"testNodeFilter()" + list.elementAt(i).toHtml());
            }

ExpandedSubBlockStart.gifContractedSubBlock.gif        }
 catch (Exception e) {
            e.printStackTrace();
        }
 
    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//*
     * 测试NodeClassFilter用法
     
*/

ExpandedSubBlockStart.gifContractedSubBlock.gif    
public void testLinkTag() {
ExpandedSubBlockStart.gifContractedSubBlock.gif        
try 
            NodeFilter filter 
= new NodeClassFilter(LinkTag.class);
            Parser parser 
= new Parser();
            parser.setURL(
"http://www.google.com");
            parser.setEncoding(parser.getEncoding());
            NodeList list 
= parser.extractAllNodesThatMatch(filter);
ExpandedSubBlockStart.gifContractedSubBlock.gif            
for (int i = 0; i < list.size(); i++{
                LinkTag node 
= (LinkTag) list.elementAt(i);
               System.out.println(
"testLinkTag() Link is :" + node.extractLink());
            }

ExpandedSubBlockStart.gifContractedSubBlock.gif        }
 catch (Exception e) {
            e.printStackTrace();
        }
 
    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//*
     * 测试<link href=” text=’text/css’ rel=’stylesheet’ />用法
     
*/

ExpandedSubBlockStart.gifContractedSubBlock.gif    
public void testLinkCSS() {
ExpandedSubBlockStart.gifContractedSubBlock.gif        
try 
            Parser parser 
= new Parser();
            parser.setInputHTML(
"<head><title>Link Test</title>"
                            
+ "<link href=’/test01/css.css’ text=’text/css’ rel=’stylesheet’ />"
                            
+ "<link href=’/test02/css.css’ text=’text/css’ rel=’stylesheet’ />"
                            
+ "</head>" + "<body>");
            parser.setEncoding(parser.getEncoding()); 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
for (NodeIterator e = parser.elements(); e.hasMoreNodes();) {
                Node node 
= e.nextNode();
               System.out.println(
"testLinkCSS()" + node.getText()
                                
+ node.getClass()); 
            }

ExpandedSubBlockStart.gifContractedSubBlock.gif        }
 catch (Exception e) {
            e.printStackTrace();
        }

    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//*
     * 测试OrFilter的用法
     
*/

ExpandedSubBlockStart.gifContractedSubBlock.gif    
public void testOrFilter() {
        NodeFilter inputFilter 
= new NodeClassFilter(InputTag.class);
        NodeFilter selectFilter 
= new NodeClassFilter(SelectTag.class); 
        NodeList nodeList 
= null
ExpandedSubBlockStart.gifContractedSubBlock.gif        
try {
            Parser parser 
= new Parser();
            parser .setInputHTML(
"<head><title>OrFilter Test</title>"
                            
+ "<link href=’/test01/css.css’ text=’text/css’ rel=’stylesheet’ />"
                            
+ "<link href=’/test02/css.css’ text=’text/css’ rel=’stylesheet’ />"
                            
+ "</head>"
                            
+ "<body>"
                            
+ "<input type=’text’ value=’text1′ name=’text1′/>"
                            
+ "<input type=’text’ value=’text2′ name=’text2′/>"
                            
+ "<select><option id=’1′>1</option><option id=’2′>2</option><option id=’3′></option></select>"
                            
+ "<a href=’http://www.yeeach.com’>yeeach.com</a>"
                            
+ "</body>"); 
            parser.setEncoding(parser.getEncoding());
            OrFilter lastFilter 
= new OrFilter();
ExpandedSubBlockStart.gifContractedSubBlock.gif            lastFilter.setPredicates(
new NodeFilter[] { selectFilter,
                    inputFilter }
);
            nodeList 
= parser.parse(lastFilter);
ExpandedSubBlockStart.gifContractedSubBlock.gif            
for (int i = 0; i <= nodeList.size(); i++{
ExpandedSubBlockStart.gifContractedSubBlock.gif                
if (nodeList.elementAt(i) instanceof InputTag) {
                    InputTag tag 
= (InputTag) nodeList.elementAt(i);
                   System.out.println(
"OrFilter tag name is :" + tag.getTagName()
                            
+" ,tag value is:" + tag.getAttribute("value"));
                }

ExpandedSubBlockStart.gifContractedSubBlock.gif                
if (nodeList.elementAt(i) instanceof SelectTag) {
                    SelectTag tag 
= (SelectTag) nodeList.elementAt(i);
                    NodeList list 
= tag.getChildren(); 
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
for (int j = 0; j < list.size(); j++{
                        OptionTag option 
= (OptionTag) list.elementAt(j);
                       System.out.println(
"OrFilter Option"
                                        
+ option.getOptionText());
                    }
 
                }

            }
 
ExpandedSubBlockStart.gifContractedSubBlock.gif        }
 catch (ParserException e) {
            e.printStackTrace();
        }

    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//*
     * 测试对<table><tr><td></td></tr></table>的解析
     
*/

ExpandedSubBlockStart.gifContractedSubBlock.gif    
public void testTable() {
        Parser myParser;
        NodeList nodeList 
= null;
        myParser 
= Parser.createParser("<body>" + "<table id=’table1′ >"
                
+ "<tr><td>1-11</td><td>1-12</td><td>1-13</td>"
                
+ "<tr><td>1-21</td><td>1-22</td><td>1-23</td>"
                
+ "<tr><td>1-31</td><td>1-32</td><td>1-33</td></table>"
                
+ "<table id=’table2′ >"
                
+ "<tr><td>2-11</td><td>2-12</td><td>2-13</td>"
                
+ "<tr><td>2-21</td><td>2-22</td><td>2-23</td>"
                
+ "<tr><td>2-31</td><td>2-32</td><td>2-33</td></table>"
                
+ "</body>""GBK");
        NodeFilter tableFilter 
= new NodeClassFilter(TableTag.class);
        OrFilter lastFilter 
= new OrFilter();
ExpandedSubBlockStart.gifContractedSubBlock.gif        lastFilter.setPredicates(
new NodeFilter[] { tableFilter });
ExpandedSubBlockStart.gifContractedSubBlock.gif        
try {
            nodeList 
= myParser.parse(lastFilter);
ExpandedSubBlockStart.gifContractedSubBlock.gif            
for (int i = 0; i <= nodeList.size(); i++{
ExpandedSubBlockStart.gifContractedSubBlock.gif                
if (nodeList.elementAt(i) instanceof TableTag) {
                    TableTag tag 
= (TableTag) nodeList.elementAt(i);
                    TableRow[] rows 
= tag.getRows(); 
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
for (int j = 0; j < rows.length; j++{
                        TableRow tr 
= (TableRow) rows[j]; 
                        TableColumn[] td 
= tr.getColumns();
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
for (int k = 0; k < td.length; k++{
                            System.out.println(
"<td>" + td[k].toPlainTextString());
                        }
 
                    }
 
//                    System.out.println(nodeList.elementAt(i)+ " "+ i);
                }

            }
 
ExpandedSubBlockStart.gifContractedSubBlock.gif        }
 catch (ParserException e) {
            e.printStackTrace();
        }

    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//*
     * 测试NodeVisitor的用法,遍历所有节点
     
*/

ExpandedSubBlockStart.gifContractedSubBlock.gif    
public void testVisitorAll() {
ExpandedSubBlockStart.gifContractedSubBlock.gif        
try {
            Parser parser 
= new Parser();
            parser.setURL(
"http://www.google.com");
            parser.setEncoding(parser.getEncoding());
ExpandedSubBlockStart.gifContractedSubBlock.gif            NodeVisitor visitor 
= new NodeVisitor() {
ExpandedSubBlockStart.gifContractedSubBlock.gif                
public void visitTag(Tag tag) {
                   System.out.println(
"testVisitorAll()  Tag name is :"
                            
+ tag.getTagName() + " \n Class is :"
                            
+ tag.getClass());
                }
 
            }

            parser.visitAllNodesWith(visitor);
ExpandedSubBlockStart.gifContractedSubBlock.gif        }
 catch (ParserException e) {
            e.printStackTrace();
        }

    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//*
     * 测试对指定Tag的NodeVisitor的用法
     
*/

ExpandedSubBlockStart.gifContractedSubBlock.gif    
public void testTagVisitor() {
ExpandedSubBlockStart.gifContractedSubBlock.gif        
try 
            Parser parser 
= new Parser(
                    
"<head><title>dddd</title>"
                            
+ "<link href=’/test01/css.css’ text=’text/css’ rel=’stylesheet’ />"
                            
+ "<link href=’/test02/css.css’ text=’text/css’ rel=’stylesheet’ />"
                            
+ "</head>" + "<body>"
                            
+ "<a href=’http://www.yeeach.com’>yeeach.com</a>"
                            
+ "</body>");
ExpandedSubBlockStart.gifContractedSubBlock.gif            NodeVisitor visitor 
= new NodeVisitor() {
ExpandedSubBlockStart.gifContractedSubBlock.gif                
public void visitTag(Tag tag) {
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
if (tag instanceof HeadTag) {
                       System.out.println(
"visitTag() HeadTag : Tag name is :"
                                
+ tag.getTagName() + " \n Class is :"
                                
+ tag.getClass() + "\n Text is :"
                                
+ tag.getText());
ExpandedSubBlockStart.gifContractedSubBlock.gif                    }
 else if (tag instanceof TitleTag) {
                       System.out.println(
"visitTag() TitleTag : Tag name is :"
                                
+ tag.getTagName() + " \n Class is :"
                                
+ tag.getClass() + "\n Text is :"
                                
+ tag.getText()); 
ExpandedSubBlockStart.gifContractedSubBlock.gif                    }
 else if (tag instanceof LinkTag) {
                       System.out.println(
"visitTag() LinkTag : Tag name is :"
                                
+ tag.getTagName() + " \n Class is :"
                                
+ tag.getClass() + "\n Text is :"
                                
+ tag.getText() + " \n getAttribute is :"
                                
+ tag.getAttribute("href"));
ExpandedSubBlockStart.gifContractedSubBlock.gif                    }
 else {
                       System.out.println(
"visitTag() : Tag name is :"
                                
+ tag.getTagName() + " \n Class is :"
                                
+ tag.getClass() + "\n Text is :"
                                
+ tag.getText());
                    }
 
                }
 
            }

            parser.visitAllNodesWith(visitor);
ExpandedSubBlockStart.gifContractedSubBlock.gif        }
 catch (Exception e) {
            e.printStackTrace();
        }

    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//*
     * 测试HtmlPage的用法
     
*/

ExpandedSubBlockStart.gifContractedSubBlock.gif    
public void testHtmlPage() {
        String inputHTML 
= "<html>"+ "<head>"
                
+ "<title>Welcome to the HTMLParser website</title>"
                
+ "</head> <body> Welcome to HTMLParser"
                
+ "<table id=’table1′ >"
                
+ "<tr><td>1-11</td><td>1-12</td><td>1-13</td>"
                
+ "<tr><td>1-21</td><td>1-22</td><td>1-23</td>"
                
+ "<tr><td>1-31</td><td>1-32</td><td>1-33</td></table>"
                
+ "<table id=’table2′ >"
                
+ "<tr><td>2-11</td><td>2-12</td><td>2-13</td>"
                
+ "<tr><td>2-21</td><td>2-22</td><td>2-23</td>"
                
+ "<tr><td>2-31</td><td>2-32</td><td>2-33</td></table>"
                
+"<form><table><tr><td>黑</td><tr></table></form><hr><br>"
                
+ "</body>" + "</html>";
        Parser parser 
= new Parser();
ExpandedSubBlockStart.gifContractedSubBlock.gif        
try {
            parser.setInputHTML(inputHTML);
            parser.setEncoding(parser.getURL());
            HtmlPage page 
= new HtmlPage(parser);
            parser.visitAllNodesWith(page%

转载于:https://www.cnblogs.com/answeryi/archive/2009/11/02/1594598.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值