获取HTML表的一部分

时间:2014-10-06 14:26:33

标签: web-scraping jsoup

我想从网站上获取表格的内容。

这是网站的源代码:

 <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.

我想获得标题。

有人可以向我解释如何获得标题,因为标题没有唯一标记吗?

2 个答案:

答案 0 :(得分:0)

标题标有 - 仅选择

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

答案 1 :(得分:0)

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

这是我的问题的解决方案