jsoup解析td标签的html网页

dongtao1010 2014-05-18 07:04:06
现在在用jsoup解析一个含有td标签的html网页,生成一个列表。。html代码如下:
<div id="divRight" style="width:744; float:right; margin-bottom:0px; margin-top:10px;text-align:center;border:0px #000000 solid;padding-right:9px;">

<table style="background-repeat:repeat-x; background-position:left bottom;" cellspacing="0" cellpadding="0" width="100%" align="center" border="0">
<tbody>
<tr>

<!-----左侧导航部分----->
<td width="100%" valign="top">
<!--///////////////////////////////////??????????内容开始????????/////////////////////////////////////////////-->
<table cellspacing="0" cellpadding="0" width="98%" border="0" style="padding-left:10px">
<tbody>
<tr>
<TD valign="top">

<link href="/Content/css.css" rel="stylesheet" type="text/css" />

<table cellspacing="0" cellpadding="0" width="98%" border="0">
<tbody>
<tr>
<td colspan="3" class="listTitle" style="text-align: left; border-bottom: 1px #999999 solid;
margin-top: 10px; line-height: 30px; vertical-align: 80%;">
通知公告
</td>
</tr>
<!--列表部分-->

<tr>
<td class="line" align="center" valign="top" width="4%" style="border-bottom: #a0a0a0 1px dashed;
line-height: 25px">
<img src="../../content/images/main_07.jpg" style="vertical-align: top;" />
</td>
<td align="left" style="border-bottom: #a0a0a0 1px dashed; line-height: 25px">

<a class="LinkStyle" target="_blank" href="/home/detail/3b110e4b-a918-419b-86d7-73221739b6da">
第三届北京市大学生书法大赛校内预选赛通知</a>

</td>
<td align="left" valign="top" width="80px" style="border-bottom: #a0a0a0 1px dashed;
line-height: 25px">
[14.03.07]
</td>
</tr>
<!-- 列表结束-->

<tr>
<td class="line" align="center" valign="top" width="4%" style="border-bottom: #a0a0a0 1px dashed;
line-height: 25px">
<img src="../../content/images/main_07.jpg" style="vertical-align: top;" />
</td>
<td align="left" style="border-bottom: #a0a0a0 1px dashed; line-height: 25px">

<a class="LinkStyle" target="_blank" href="/home/detail/58955942-a526-4805-9026-5b511af26d8d">
第五期人际关系体验及成长小组招募(仅限雁栖湖校区)</a>

</td>
<td align="left" valign="top" width="80px" style="border-bottom: #a0a0a0 1px dashed;
line-height: 25px">
[14.02.26]
</td>
</tr>
<!-- 列表结束-->

<tr>
<td class="line" align="center" valign="top" width="4%" style="border-bottom: #a0a0a0 1px dashed;
line-height: 25px">
<img src="../../content/images/main_07.jpg" style="vertical-align: top;" />
</td>
<td align="left" style="border-bottom: #a0a0a0 1px dashed; line-height: 25px">

<a class="LinkStyle" target="_blank" href="/home/detail/1945d023-b211-43d5-bdac-7f01169342f7">
第四期人际关系体验及成长小组招募</a>

</td>
<td align="left" valign="top" width="80px" style="border-bottom: #a0a0a0 1px dashed;
line-height: 25px">
[14.02.26]
</td>
</tr>


我想将上面内容以列表的形式显示出来,下面是我解析新闻用的代码:
public void newsListAnalysis(String nowPlateUrl) {
mNewsList= new ArrayList<News>();
try {
Document doc = Jsoup.connect(nowPlateUrl).timeout(4000).get();
Elements divElement=doc.getElementById("divRight").select("table").get(2).select("tbody");

for(Element it:divElement)
{
News mNews=new News();
String a=it.select("td").get(2).select("a").attr("href").trim();
String c=it.select("td").get(2).select("a").text();
String b=it.select("td").get(3).text();

mNews.setmUrl(a);
mNews.setmDate(b);
mNews.setmTitle(c);
System.out.println("a:"+a+",b:"+b+"c:"+c);
mNewsList.add(mNews);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

但是最后结果我只显示了一条消息,截图如下



请问怎么把更改代码能够满足我的要求,还是说我对jsoup理解的不够。。。
...全文
650 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
dongtao1010 2014-05-19
  • 打赏
  • 举报
回复
求大神,我急着解决这个问题啊
dongtao1010 2014-05-19
  • 打赏
  • 举报
回复
没人回答吗???请大神进来啊
tony4geek 2014-05-19
  • 打赏
  • 举报
回复
Document doc = null;
		 Elements elements = null;
		 String html="<table cellspacing=\"0\" cellpadding=\"0\" width=\"98%\" border=\"0\">";
		 html+="<tbody>";
		 html+="<tr>";
		 html+="<td colspan=\"3\" class=\"listTitle\" style=\"text-align: left; border-bottom: 1px #999999 solid;";
		 html+=" margin-top: 10px; line-height: 30px; vertical-align: 80%;\">";
		 html+=" 通知公告";
		 html+="</td>";
		 html+="</tr>";
		 html+=" <!--列表部分-->";
       
		 html+="<tr>";
		 html+="<td class=\"line\" align=\"center\" valign=\"top\" width=\"4%\" style=\"border-bottom: #a0a0a0 1px dashed;";
		 html+="      line-height: 25px\">";
		 html+="<img src=\"../../content/images/main_07.jpg\" style=\"vertical-align: top;\" />";
		 html+="</td>";
		 html+="<td align=\"left\" style=\"border-bottom: #a0a0a0 1px dashed; line-height: 25px\">";
               
		 html+="<a class=\"LinkStyle\" target=\"_blank\" href=\"/home/detail/3b110e4b-a918-419b-86d7-73221739b6da\">";
		 html+="第三届北京市大学生书法大赛校内预选赛通知</a>";
               
		 html+="</td>";
		 html+="<td align=\"left\" valign=\"top\" width=\"80px\" style=\"border-bottom: #a0a0a0 1px dashed;";
		 html+="line-height: 25px\">";
		 html+="[14.03.07]";
		 html+=" </td>";
		 html+="</tr>";
		 html+="<!-- 列表结束-->";
       
		 html+="<tr>";
		 html+="<td class=\"line\" align=\"center\" valign=\"top\" width=\"4%\" style=\"border-bottom: #a0a0a0 1px dashed;";
		 html+="line-height: 25px\">";
		 html+=" <img src=\"../../content/images/main_07.jpg\" style=\"vertical-align: top;\" />";
		 html+="</td>";
		 html+="<td align=\"left\" style=\"border-bottom: #a0a0a0 1px dashed; line-height: 25px\">";
               
		 html+="<a class=\"LinkStyle\" target=\"_blank\" href=\"/home/detail/58955942-a526-4805-9026-5b511af26d8d\">";
		 html+="第五期人际关系体验及成长小组招募(仅限雁栖湖校区)</a>";
		 html+="</td>";
			 html+="<td align=\"left\" valign=\"top\" width=\"80px\" style=\"border-bottom: #a0a0a0 1px dashed;";
		 html+="line-height: 25px\">";
		 html+="[14.02.26]";
		 html+="</td>";
		 html+="</tr>";
			 html+="<!-- 列表结束-->";
       
		 html+="<tr>";
			 html+="<td class=\"line\" align=\"center\" valign=\"top\" width=\"4%\" style=\"border-bottom: #a0a0a0 1px dashed;";
		 html+="line-height: 25px\">";
		 html+="<img src=\"../../content/images/main_07.jpg\" style=\"vertical-align: top;\" />";
		 html+="</td>";
		 html+="<td align=\"left\" style=\"border-bottom: #a0a0a0 1px dashed; line-height: 25px\">";
               
		 html+="<a class=\"LinkStyle\" target=\"_blank\" href=\"/home/detail/1945d023-b211-43d5-bdac-7f01169342f7\">";
		 html+=" 第四期人际关系体验及成长小组招募</a>";
               
		 html+="</td>";
		 html+="<td align=\"left\" valign=\"top\" width=\"80px\" style=\"border-bottom: #a0a0a0 1px dashed;";
		 html+=" line-height: 25px\">";
		 html+=" [14.02.26]";
		 html+="</td>";
		 html+="</tr>";
		 html+="</tbody>";
		 html+="</table>";
		 doc = Jsoup.parse(html) ;
		 elements = doc.select("table tr td:gt(1)");
		 for (Element p : elements) {
			 System.out.println(p.parent().select("td a").attr("href")+"-->"+p.parent().select("td a").text()+"---->"+p.parent().select("td:eq(2)").text());
		 }
tony4geek 2014-05-19
  • 打赏
  • 举报
回复
是不是得是这样的格式 <div id="divRight"> <table> 0 </table> <table> 1 </table> <table> 2 你的数据 </table> </div>
dongtao1010 2014-05-19
  • 打赏
  • 举报
回复
引用 4 楼 rui888 的回复:
标签 select 获取的方式不对。
怎么获取,能详细解释一下吗???
tony4geek 2014-05-19
  • 打赏
  • 举报
回复
标签 select 获取的方式不对。
dongtao1010 2014-05-18
  • 打赏
  • 举报
回复
哪个大神进来回答一下啊,,求你们了。。。

80,358

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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