请熟悉HtmlParser(或了解分析html)的朋友进来,万分感谢
本帖最后由 xiongchangjing 于 2010-10-23 21:34:35 编辑 需要做一个功能,就是分析一个得到的html,提取出需要的数据并且插入到数据库中,之前用HttpClient Post提交到一个网站上查询数据,已经返回html,内容如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
省略代码 .............................
</head>
<body>
<div style=" width:960px; margin:0 auto;background:#fafafa;">
<div style="width:960px; margin:0 auto;">
<!-- 头部部分 -->
省略代码.....................................
<!-- 头部部分结束 -->
<!-- -->
<div id="detaiInforsTag">
<a href="/index.do">主页 </a> >
交通违章查询
</div>
<!-- 查询输入框 -->
<div class="queryOuterBox">
<div align="center"
style="width: 920px; height: 120px; padding-bottom: 8px">
<a href="http://www.dgzyyz.com/"><img
src="/images/query/zyyzindex.gif" />
</a>
</div>
<div class="querymain">
<div class="rightbian">
<table width="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td align="left" valign="bottom">
<img
src="/images/query/jtwz.gif" /></td>
</tr>
<tr>
<td align="left" valign="top">
<div class="queryContentBox2">
<form method="post"
action="/query.searchJtwzResult.do">
<table width="100%" border="0" cellspacing="0" cellpadding="5">
<tr>
<td width="250" height="40" align="right">
选择车辆类型:
</td>
<td align="left">
<select name="carType" class="inputText">
<option value="小型汽车">
小型汽车
</option>
<option value="大型汽车">
大型汽车
</option>
<option value="外籍汽车">
外籍汽车
</option>
</select>
</td>
</tr>
<tr>
<td height="40" align="right">
输入车辆号码:
</td>
<td align="left">
<input type="text" name="carNumber" class="inputText"
value="粤s898bf" />
</td>
</tr>
<tr>
<td height="40" align="right">
输入车架号码(后四位):
</td>
<td align="left">
<input type="text" name="carCode" class="inputText"
value="8968" />
</td>
</tr>
<tr>
<td height="40" colspan="2" align="center">
<input type="submit" value="查询违章" />
</td>
</tr>
</table>
</form>
</div>
</td>
</tr>
</table>
</div>
<div class="midbian">
<table width="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td align="left" valign="middle">
<img
src="/images/query/zy.gif" />
</td>
</tr>
<tr>
<td>
<div class="queryContentBox">
<form method="post" id="carsInforsForm"
action="/others/inPost.jsp">
<input type="hidden" name="bs" value="0" />
<table width="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="80" height="30" align="right">
姓名:
</td>
<td>
<label>
<input type="text" name="xm">
</label>
</td>
<td width="80" style="color: #FF0000">
*(必填)
</td>
</tr>
<tr>
<td height="30" align="right">
手机号码:
</td>
<td>
<label>
<input type="text" name="sjhm">
</label>
</td>
<td style="color: #FF0000">
*(必填)
</td>
</tr>
<tr>
<td height="30" align="right">
车牌号码:
</td>
<td>
<label>
<input type="text" name="cphm" />
</label>
</td>
<td style="color: #FF0000">
*(必填)
</td>
</tr>
<tr>
<td height="30" align="right" valign="middle">
车辆类型:
</td>
<td>
<label>
<select name="cllx" class="inputText">
<option value="小型汽车">
小型汽车
</option>
<option value="大型汽车">
大型汽车
</option>
<option value="外籍汽车">
外籍汽车
</option>
</select>
</label>
</td>
<td style="color: #FF0000">
*(必填)
</td>
</tr>
<tr>
<td height="30" align="right" valign="middle">
车架后四位:
</td>
<td>
<label>
<input type="text" name="cjh" maxlength="4" />
</label>
</td>
</tr>
<tr>
<td height="30" align="right" valign="middle">
联系地址:
</td>
<td>
<label>
<input type="text" name="lxdz" />
</label>
</td>
<td style="color: #FF0000">*车管家必填</td>
</tr>
<tr>
<td height="40"> </td>
<td height="40" valign="middle">
<a href="#" onClick="carsInforsFormSubmit(1);"> <img
src="/images/query/cgjannu.gif" /></a>
<a href="#" onClick="carsInforsFormSubmit(0);"> <img
src="/images/query/cxtannu.gif" /></a></td>
</tr>
<tr>
<td height="15"> </td>
<td height="15"><font color="#FF0000">免费上门资料揽收 免费体验</font></td>
<td> </td>
</tr>
<tr>
<td> </td>
<td height="30" align="center" valign="middle"><a href="http://www.dgpost.com.cn/cxt/login.jsp"><font color="#2b5808">管理员登录</font></a></font></td>
<td> </td>
</tr>
</table>
</form>
</div>
</td>
</tr>
</table>
</div>
</div>
</div>
<!-- 查询和结果分割区域 -->
<div class="clearBlank0" style="height: 0px"></div>
<!-- 查询内容框 -->
<div>
<table width="100%" border="0" cellspacing="0" cellpadding="0"
class="inforsShowPage" style="empty-cells: show">
<caption
style="text-align: right; padding: 5px; font-weight: bold;">
[共查询到
<font color='red'>3</font> 条违章数据]
</caption>
<tr>
<th
style="border-left: 1px solid #cccccc; border-top: 1px solid #cccccc;">
凭证号
</th>
<th style="border-top: 1px solid #cccccc;">
违法代码
</th>
<th style="border-top: 1px solid #cccccc;">
违章事件
</th>
<th style="border-top: 1px solid #cccccc;">
违章时间
</th>
<th style="border-top: 1px solid #cccccc;">
违章地点
</th>
<th style="border-top: 1px solid #cccccc;">
违章缴纳费用
</th>
</tr>
<tr>
<td style="border-left: 1px solid #cccccc">
44191400P642831
</td>
<td>
1303
</td>
<td>
机动车行驶超过规定时速50%以下的
</td>
<td>
2010-09-24 15:24:21.0
</td>
<td>
塘厦蛟平路科苑城路段
</td>
<td>
150.0
</td>
</tr>
<tr>
<td style="border-left: 1px solid #cccccc">
44192500P579896
</td>
<td>
1303
</td>
<td>
机动车行驶超过规定时速50%以下的
</td>
<td>
2010-09-11 16:08:46.0
</td>
<td>
公常线治安工作站路段
</td>
<td>
150.0
</td>
</tr>
<tr>
<td style="border-left: 1px solid #cccccc">
44191400M890826
</td>
<td>
1230
</td>
<td>
机动车违反禁止标线指示的
</td>
<td>
2010-02-03 16:38:50.0
</td>
<td>
东莞市塘厦镇沙湖路
</td>
<td>
200.0
</td>
</tr>
</table>
</div>
<!-- -->
<div class="clearBlank0" style="wdith:100%;"></div>
<!-- foot Part -->
<div id="footBanner">
<div id="FootTitle"></div>
</div>
</div>
</div>
</body>
</html>
我需要获取的是
<!-- 查询内容框 -->
<div>
<table width="100%" border="0" cellspacing="0" cellpadding="0"
class="inforsShowPage" style="empty-cells: show">
<caption
style="text-align: right; padding: 5px; font-weight: bold;">
[共查询到
<font color='red'>3</font> 条违章数据]
</caption>
省略代码,需取里面的信息..........
</table>
</div>
中td里面的内容,里面是循环了一个tr
本人从未接触过HtmlParser,API看不太懂,英语不好....
谁能给点分析思想,如果有例子那更好,谢谢了
[解决办法]
我用过一种土办法:
先把每行代码trim后拼装,整个网页就成一个大String,然后使用正则表达式根据特征来提取信息。
如果网页信息很复杂的话,可以用正则表达式先进行粗提,然后在里面使用正则再提一次…
[解决办法]
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Test {
public static void main(String[] args){
String str="<tr>" +
"<th style=\"border-left: 1px solid #cccccc; border-top: 1px solid #cccccc;\">凭证号</th>" +
"<th style=\"border-top: 1px solid #cccccc;\">违法代码</th>" +
"<th style=\"border-top: 1px solid #cccccc;\">违章事件</th>" +
"<th style=\"border-top: 1px solid #cccccc;\">违章时间</th>" +
"<th style=\"border-top: 1px solid #cccccc;\">违章地点</th>" +
"<th style=\"border-top: 1px solid #cccccc;\">违章缴纳费用</th>" +
"</tr>" +
"<tr>" +
"<td style=\"border-left: 1px solid #cccccc\">" +
"44191400P642831</td>" +
"<td>1303</td>" +
"<td>机动车行驶超过规定时速50%以下的</td>" +
"<td>2010-09-24 15:24:21.0</td><td>" +
"塘厦蛟平路科苑城路段</td>" +
"<td>150.0</td>" +
"</tr>" +
"<tr>" +
"<td style=\"border-left: 1px solid #cccccc\">44192500P579896</td>" +
"<td>1303</td>" +
"<td> 机动车行驶超过规定时速50%以下的</td>" +
"<td>2010-09-11 16:08:46.0</td>" +
"<td>公常线治安工作站路段</td>" +
"<td>150.0</td>" +
"</tr>" +
"<tr>" +
"<td style=\"border-left: 1px solid #cccccc\">44191400M890826" +
"</td>" +
"<td>1230</td>" +
"<td>机动车违反禁止标线指示的</td>" +
"<td>2010-02-03 16:38:50.0</td>" +
"<td>东莞市塘厦镇沙湖路</td>" +
"<td>200.0</td>" +
"</tr>";
Matcher m=Pattern.compile("<t[hd][^<>]*>([^<>]*)</t[hd]>",Pattern.DOTALL).matcher(str);
int count=0;
while(m.find()){
count++;
System.out.print(m.group(1));
System.out.print(" ");
if(count==6){
System.out.println();
count=0;
}
}
}
}