mlevytskiy
mlevytskiy

Reputation: 1665

How find and select tag in html with Jsoup?

This part of text from get response http://youtube.com/. How parse response and get icon with 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">

Upvotes: 0

Views: 527

Answers (1)

Rodolfo
Rodolfo

Reputation: 1171

If you want to get all href values:

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

In the case you need to select only the link with a specific rel attribute you could switch "link" by:

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

Upvotes: 2

Related Questions