JSOUP:如何获取Href?

JSOUP: How to get Href?

我有这个 HTML 代码:

 <td class="topic starter"><a href="http://www.test.com">Title</a></td>

我想提取 "Title" 和 URL,所以我这样做了:

 Elements titleUrl = doc.getElementsByAttributeValue("class", "topic starter");
 String title = titleUrl.text();

这适用于标题,但对于 URL 我尝试了以下方法:

 String url = titleUrl.html();
 String url = titleUrl.attr("a [href]");
 String url = titleUrl.attr("a[href]");
 String url = titleUrl.attr("href");
 String url = titleUrl.attr("a");

但是没有人工作,我无法得到 URL。

试试这个:

Element link = doc.select("td.topic.starter > a").first();
String url = link.attr("href");

您首先 select a 元素,然后提取其属性 href