2014-10-06 76 views
0

我想从网站获取表格的内容。获取HTML表格的一部分

这是网站的源代码:

<tr><td><table width='100%'><tr><td valign='top' width='1px' class='GridViewRow1'><img src='/images/pin.gif'></td><td class='GridViewRow1'><a href='Announcements.etc'><b><i>Title num 1</i></b></a><div class='SmallText'>Username</div><div class='SmallText' style='color:#808080;'>date</div></td></tr></table></td></tr> 
<tr><td><table width='100%'><tr><td valign='top' width='1px' class='GridViewRow1'><img src='/images/pin.gif'></td><td class='GridViewRow1'><a href='Announcements.etc2'><b><i>Title num 2</i></b></a><div class='SmallText'>username</div><div class='SmallText' style='color:#808080;'>date</div></td></tr></table></td></tr> 

所以这是我的代码

Document doc = Jsoup.connect(url).get(); 
Elements td = doc.select("td.GridViewRow1"); 
desc = td.get(0).nextElementSibling().text(); 

我得到的输出是:

Title num 1 username date as a string. 

我想拿到冠军只要。

有人可以向我解释如何获得标题,因为标题没有独特的标签?

回答

0
Document doc = Jsoup.connect(url).get(); 
Elements td = doc.select("td.GridViewRow1"); 
desc = td.select("a[href]").first().text(); 

这是解决我的问题

+0

你可以简化为desc = doc.select(“td.GridViewRow1一个[HREF ]“)第一()文本()。; – 2014-10-10 05:55:08

0

标题标有 - 只选择

... td = doc.select("td.GridViewRow1 > b >i");