急!HtmlParser提取文本的问题,求教

sjc12 2010-05-12 09:28:34
我用HtmlParser在《开发自己的搜索引擎》基础上想提取
http://product.pconline.com.cn/notebook/lenovo/261638_detail.html 里的参数,把他们存到txt里,但那个类始终有问题,不知有没有牛人能够帮个忙?
...全文
218 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
SelfMedicated 2012-03-13
  • 打赏
  • 举报
回复
楼主,http://blog.sina.com.cn/s/blog_3c6ecea90100iub1.html,希望对你有帮助
sjc12 2010-05-12
  • 打赏
  • 举报
回复
希望能帮我弄弄,比较急,我也是初学者,谢谢了
sjc12 2010-05-12
  • 打赏
  • 举报
回复
不好意思 就一个方法
public void extract() {
BufferedWriter bw = null;
//创建属性过滤器
NodeFilter attributes_filter = new AndFilter(new TagNameFilter("td"),
new HasAttributeFilter("WIDTH", "16%"));
//创建标题过滤器
NodeFilter title_filter = new AndFilter(new TagNameFilter("span"),
new HasAttributeFilter("class", "mark")
);

//提取标题信息
try {
//Parser根据过滤器返回所有满足过滤条件的节点
NodeList title_nodes = this.getParser().parse(title_filter);
//遍历所有节点
for (int i = 0; i < title_nodes.size(); i++) {
TableColumn node = (TableColumn) title_nodes.elementAt(i);
//用空格分割节点内胡html文本
String[] names = node.getChildrenHTML().split(" ");
StringBuffer title = new StringBuffer();
//创建要生成的文本文件名
for (int k = 0; k < names.length; k++) {
title.append(names[k]).append("-");
}
title.append((new Date()).getTime());
//创建要生成的文件
bw = new BufferedWriter(new FileWriter(new File(this
.getOutputPath()
+ title + ".txt")));
//获取当前提取页的完整URL地址
int startPos = getInuputFilePath().indexOf("mirror") + 6;
String url_seg = getInuputFilePath().substring(startPos);
url_seg = url_seg.replaceAll("\\\\", "/");
String url = "http:/" + url_seg;
System.out.println(url);
//写入当前提取页的完整URL地址
bw.write(url + NEWLINE);
bw.write(names[0] + NEWLINE);
bw.write(names[1] + NEWLINE);
}
} catch (Exception e) {
e.printStackTrace();
}
//重置Parser
this.getParser().reset();
try {
//Parser根据过滤器返回所有满足过滤条件的节点
NodeList attributes_nodes = this.getParser().parse(attributes_filter);
for (int i = 0; i < attributes_nodes.size(); i++) {
//Parser根据过滤器返回所有满足过滤条件的节点
TableColumn node = (TableColumn) attributes_nodes.elementAt(i);
String text = node.getChildrenHTML();
//提取属性名信息
String result = getProp(
"<TD width=\"16%\" class=\"#FCFCFC\">(.*)</TD>",
node.toHtml(), 1);
//属性里面包含有link标签的情况
if (result.indexOf("<") != -1)
result = getProp(
"<TD CLASS=btd WIDTH=198 BGCOLOR=\"#FCFCFC\"(.*)>(.*)</a></B></TD>",
node.toHtml(), 2);
//提取属性值信息
TableColumn nodeExt = (TableColumn) node.getNextSibling()
.getNextSibling();
bw.write(StringUtils.trim(result) + ":"
+ StringUtils.trim(nodeExt.getChildrenHTML()));
bw.newLine();
continue;
}
} catch (Exception e) {
e.printStackTrace();
}
amdgaming 2010-05-12
  • 打赏
  • 举报
回复
你说的清楚点啊? 比如把你类 发出来

你这样没头没脑 谁 能回答出你的问题啊

62,621

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧