html放入一个String中,将每行<td class = "b"></td>中的值读出

我现在有一个网页。
<html>
<body>
<div>31423423423
</div>
<div>
<table>
<tr>
<td class = "a">asdfdas</td><th>234</th><td class = "b">12</td>
</tr>
<tr>
<td class = "a">asdfdas</td><th>234</th><td class = "b">123434</td>
</tr>
<tr>
<td class = "a">asdfdas</td><th>234</th><td class = "b">132</td>
</tr>
</table>
</div>
</body>
</html>

 

import java.util.*;
import java.io.*;
import java.util.regex.*;

public class TableTest {

public static void main(String[] args) {
String rex = "<\\s*td\\s+class\\s+=\\s+\"\\s*b\\s*\"\\s*>(.*)<\\s*/td\\s*>";
//指定要提取的网页
String path = "c:/test.html";
Pattern p = Pattern.compile(rex);
Matcher m = null;
StringBuffer sb = new StringBuffer();
String temp = null;
try {
File f = new File(path);
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(f)));
while((temp = reader.readLine()) != null) {
m = p.matcher(temp);
while(m.find()) {
System.out.println(m.group(1));
sb.append(m.group(1) + "!");
}
}
} catch (Exception e) {
}
/////res就是保存的结果
/*
String [] res = sb.toString().split("!");
for(String s: res) {
System.out.println(s);
}
*/
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值