Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Java SE 综合讨论区  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 请教(关于htmlpaser开源包的)
fishman





发贴: 16
积分: 0
于 2005-04-20 03:16 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
我做了一个程序,就是从一个html解析出:
1、获取<a>标签
2、取得值为key(汽车)的<a>标签
3、取<a>标签中的href属性的值

我写了一个程序,结果能解析163网站的东西,但是sina和sohu的都解析不了,程序debug时有错,说找不到org.htmlparser.util里面的IterotarImpl.class,我甚是纳闷,那怎么能解析163的呢?请对htmlparser有研究的高人指点一下。
程序大致如下:

import org.htmlparser.*;
import org.htmlparser.util.NodeIterator;
import org.htmlparser.lexer.nodes.TagNode;

/**
* @author zhangyu
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class ParserHref {
  public static void main(String[] args)throws Exception
  {
     //取得所有的标签对应的Node
     Parser parser = new Parser("http://www.163.com");
     NodeIterator iterator = parser.elements();
     Node node = iterator.nextNode();
     //调用一个递归的遍历方法
     getLinks(node);

  }

   //递归遍历所有的node节点内满足要求的节点
   public static void getLinks(Node pNode)
      {
     
        for (int i = 0; i<pNode.getChildren().size(); i++)
     
      {
      Node node = pNode.getChildren().elementAtLight Bulb;
      if (node instanceof TagNode)
      {
      TagNode tag = (TagNode) node;
      //判断内容为“汽车”的标签为A的节点
      if(tag.getTagName().equals("A") &&tag.toPlainTextString().equals("汽车"))
      {
      //取得,并打印满足要求节点的“href”属性
      System.out.println("汽车:" + tag.getAttribute("href"));
      }
      if (tag.getChildren() != null && tag.getChildren().size() > 0)
      getLinks(tag);
      }
      }  
      }

}




话题树型展开
人气 标题 作者 字数 发贴时间
3463 请教(关于htmlpaser开源包的) fishman 1538 2005-04-20 03:16

flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923