如何使用Jsoup在html中查找和选择标记?

时间:2016-02-03 21:47:37

标签: java jsoup

来自回复http://youtube.com/的这部分文字。 如何使用Jsoup

解析响应并获取图标
<link rel="shortcut icon" href="https://s.ytimg.com/yts/img/favicon-vflz7uhzw.ico" type="image/x-icon"> 
      <link rel="icon" href="//s.ytimg.com/yts/img/favicon_32-vfl8NGn4k.png" sizes="32x32">
      <link rel="icon" href="//s.ytimg.com/yts/img/favicon_48-vfl1s0rGh.png" sizes="48x48">
      <link rel="icon" href="//s.ytimg.com/yts/img/favicon_96-vfldSA3ca.png" sizes="96x96">
      <link rel="icon" href="//s.ytimg.com/yts/img/favicon_144-vflWmzoXw.png" sizes="144x144">

1 个答案:

答案 0 :(得分:2)

如果您想获得所有href值:

List<String> href = new ArrayList<>();
// Considering that text is a String variable that contains the html
final Document document = Jsoup.parse(text);
for (Element element : document.select("link")) {
    href.add(element.attr("href"));
}
// In this point the list href will have all the links

如果您只需要选择具有特定rel属性的链接,则可以切换&#34; link&#34;由:

"link[rel~=\"icon\"]" //could have 1 or more rel values
"link[rel=\"icon\"]" //rel equals to icon