我有这个 HTML 代码:
<td class="topic starter"><a href="http://www.test.com">Title</a></td>
我想提取“标题”和 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");
您首先选择a
元素,然后提取其属性href
。
关于java - JSOUP:如何获取Href?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30408174/