java JSOUP:如何获得Href?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30408174/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 17:04:00 来源:igfitidea点击:
JSOUP: How to get Href?
提问by helloimyourmind
I've this HTML code:
我有这个 HTML 代码:
<td class="topic starter"><a href="http://www.test.com">Title</a></td>
I want to extract "Title" and the URL, so I did this:
我想提取“标题”和 URL,所以我这样做了:
Elements titleUrl = doc.getElementsByAttributeValue("class", "topic starter");
String title = titleUrl.text();
And this works for the title, but for the URL I tried the following:
这适用于标题,但对于 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");
But no one works and I'm not able to get the URL.
但是没有人工作,我无法获得 URL。
回答by Timo
Try this:
试试这个:
Element link = doc.select("td.topic.starter > a");
String url = link.attr("href");
You first select the a
element and then extract its attribute href
.
您首先选择a
元素,然后提取其属性href
。