waldemar
waldemar

Reputation: 695

get favicon from html (JSOUP)

How can I get icon path from html string uses JSOUP?

I find to diferent way to add favicon on webpage -

(in Google)

first method I can to get uses doc.select("html head meta")

but I can't to select link tag

Upvotes: 3

Views: 3134

Answers (3)

Aditya Kamat
Aditya Kamat

Reputation: 149

It's pretty late to submit a answer but correct way to check is "rel" tag

 public boolean checkFevicon() {
        Elements e = doc.head().select("link[rel=shortcut icon]");
        if (e.isEmpty()) {
            return false;
        } else {
            return true;
        }
    }

jQuery equivalent

  $("link[rel='shortcut icon']")

Upvotes: 3

surfealokesea
surfealokesea

Reputation: 5116

Get the file name on head element:

Connection con2=Jsoup.connect(url);
Document doc = con2.get();
Element e=doc.head().select("link[href~=.*\\.ico]").first();
String url=e.attr("href");

http://jsoup.org/cookbook/extracting-data/attributes-text-html

http://jsoup.org/cookbook/extracting-data/selector-syntax

Upvotes: 5

Enrichman
Enrichman

Reputation: 11337

As Uwe Plonus pointed out in the comment you can always get the favicon from <website>/favicon.ico

Google favicon

Upvotes: 3

Related Questions