获取HTML表的一部分

问题描述 投票:0回答:2

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

这是网站的源代码:

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

我想获得标题。

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

web-scraping jsoup
2个回答
0
投票

标题标有 - 选择那个

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

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

这是我的问题的解决方案

© www.soinside.com 2019 - 2024. All rights reserved.