`
kakaluyi
  • 浏览: 449463 次
  • 性别: Icon_minigender_1
  • 来自: 苏州
社区版块
存档分类
最新评论

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);
}
*/
}
}

0
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics