196

Simple enough question: I've created a small app that is basically just a favourites that sits in my system tray so that I can open often-used sites/folders/files from the same place. Getting the default icons from my system for known file types isn't terribly complicated, but I don't know how to get the favicon from a website. (SO has the grey->orange stack icon in the address bar for instance)

Does anyone know how I might go about that?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Steven Evers
  • 16,649
  • 19
  • 79
  • 126
  • 1
    I'm not sure how easy (or possible) it would be to automate the loading/parsing of this page, but it seems to have everything: [Favicon-Checker](https://realfavicongenerator.net/favicon_checker#.XSI6p6KtiuP). At the very least, you can use it as a reference/check. – Kevin Fegan Jul 07 '19 at 18:38
  • You could just use the [Statvoo Favicon API](https://favicons.statvoo.com), that'd be pretty quick and painless. – AO_ Jan 30 '20 at 15:43
  • 1
    [Favicon Kit](https://faviconkit.com/?ref=stackoverflow) lets you get & embed favicons like regular images, in [sizes much larger than 16 pixels](https://api.faviconkit.com/stackoverflow.com/144), if available. (Disclosure: I’m the author) – AndreasPizsa Feb 29 '20 at 21:29

17 Answers17

359

You'll want to tackle this a few ways:

  1. Look for the favicon.ico at the root of the domain

    www.domain.com/favicon.ico

  2. Look for a <link> tag with the rel="shortcut icon" attribute

    <link rel="shortcut icon" href="/favicon.ico" />

  3. Look for a <link> tag with the rel="icon" attribute

    <link rel="icon" href="/favicon.png" />

The latter two will usually yield a higher quality image.


Just to cover all of the bases, there are device specific icon files that might yield higher quality images since these devices usually have larger icons on the device than a browser would need:

<link rel="apple-touch-icon" href="images/touch.png" />

<link rel="apple-touch-icon-precomposed" href="images/touch.png" />


And to download the icon without caring what the icon is you can use a utility like http://www.google.com/s2/favicons which will do all of the heavy lifting:

var client = new System.Net.WebClient();

client.DownloadFile(
    @"http://www.google.com/s2/favicons?domain=stackoverflow.com",
    "stackoverflow.com.ico");
NoDataDumpNoContribution
  • 10,591
  • 9
  • 64
  • 104
hunter
  • 62,308
  • 19
  • 113
  • 113
  • 2
    does GetFavIcon still work? when I run your example I get a 400 error – Julien Apr 19 '15 at 15:43
  • 34
    looks like Google has a similar service now: http://www.google.com/s2/favicons?domain_url=stackoverflow.com – hunter May 08 '15 at 19:49
  • 39
    if anyone wants an alternative to Google, DuckDuckGo has another solution: http://icons.duckduckgo.com/ip2/www.stackoverflow.com.ico – Jose Serodio Jul 10 '16 at 00:51
  • Thanks so much, option 1 worked for me, after searching for 20 minutes! – Philippe Genois Aug 31 '17 at 23:59
  • 1
    @hunter `google.com/s2/favicons` it is an icon with 16x16 it is terrible icon do you know where to take larger icon ? – TheCoderGuy Feb 13 '19 at 09:21
  • @JoseSerodio - The link you provided works as shown, but how do you format the domain you're checking? I mean the part after "/ip2/". – Kevin Fegan Jul 07 '19 at 18:44
  • @KevinFegan if the website is "eldiario.es" just https://icons.duckduckgo.com/ip2/eldiario.es.ico ... just after /ip2/ add the website and then ".ico" – Jose Serodio Jul 07 '19 at 18:53
  • 3
    @JoseSerodio - Well... that was the first thing I tried, but for the domain I was checking, I just got a "dim/dull" image of a right pointing arrow (greater-than) inside a circle-inside a square. [It looked like this](https://icons.duckduckgo.com/ip2/.ico), but I checked to make sure I had "domain.com.ico" spelled correctly. Now, by the wonders of the internet, it's working correctly... go figure =) so, Nevermind. – Kevin Fegan Jul 07 '19 at 19:08
  • 1
    2 & 3 amount to the same: finding `` with `icon` within whitespace-separated keywords. I recommend not using any 3rd-party *service*, even Google, as it could be discontinued at any moment. By all means use a 3rd-party *library* to do the scraping and caching of results. You must also hotlink (not copy) the image to avoid copyright infringement. – Jake Jul 31 '20 at 02:08
  • 7
    @Abedin.Zhuniqi add the parameter `&sz=:size` where `:size` is the size in px, e.g. [`https://www.google.com/s2/favicons?sz=32&domain_url=stackoverflow.com`](https://www.google.com/s2/favicons?sz=32&domain_url=stackoverflow.com) – Aryan Beezadhur Nov 25 '20 at 14:10
  • the favicon.ico works nice, you just need to be sure to ba at website root : cut the path from https://stackoverflow.com/questions/5119041/how-can-i-get-a-web-sites-favicon to https://stackoverflow.com/favicon.ico. You'll need a little extension or helper method for that. the use : using (var client = new WebClient()) { client.DownloadFile(new Uri(favIcoPath), localPath); if (File.Exists(path)) return path.ToBitmapImage(); } – L Chougrani Jan 13 '21 at 08:49
  • @JoseSerodio How to ommit placeholder icon for unknown domain, so I could try to fetch from another service in "onerror" listener? – emvaized May 28 '21 at 03:04
  • @AryanTYB Are there public documentation about which parameters can be used? – kunambi May 10 '23 at 19:06
59

Updated 2020

Here are three services you can use in 2020 onwards

<img height="16" width="16" src='https://icons.duckduckgo.com/ip3/www.google.com.ico' />

<img height="16" width="16" src='http://www.google.com/s2/favicons?domain=www.google.com' />

<img height="16" width="16" src='https://api.statvoo.com/favicon/?url=google.com' />
Blowsie
  • 40,239
  • 15
  • 88
  • 108
  • 5
    grabicon.com is now paid service starting from $9/mo – janot Feb 19 '16 at 18:07
  • 1
    Another one: http://favicon.allesedv.com/ – Martin Seitl Aug 09 '16 at 14:56
  • This service no-longer works, you can use https://api.statvoo.com/favicon/?url=stackoverflow.com. I've used it for a couple of years and it's never let me down. – AO_ Jan 30 '20 at 15:47
  • 1
    Thanks @AO_, im actually using duckduckgo these days, Ive updated my answer – Blowsie Feb 03 '20 at 15:32
  • 1
    @AO_ then stavoo api is just a wrapper to the google service – vixalien May 10 '20 at 13:20
  • 1
    @saintvixalien I believe this has recently changed for people who don't initialise with an API key, as the servers were getting pounded ;) I use the primary service with an API key and get responses directly without the wrapper. I did test without an API key and it redirects to Google's service as you said.. – AO_ May 11 '20 at 17:19
  • @MartinSeitl `allesedv` is no longer online, you can't use that any more. Best I think is to scrape the HTML `head` for `` and hotlink and cache the best result. – Jake Jul 31 '20 at 01:42
  • @jake thx 4 your comment! favicon.allesedv.com is online again. – Martin Seitl Aug 01 '20 at 19:34
  • How to ommit placeholder icon for unknown domain, so I could try next service in "onerror" listener? – emvaized May 28 '21 at 03:02
  • api.statvoo.com seems to just redirect to Google's S2 service. – Akito Oct 09 '21 at 20:53
18

You can use Google S2 Converter.

http://www.google.com/s2/favicons?domain=google.com

Source: http://www.labnol.org/internet/get-favicon-image-of-websites-with-google/4404/

raminjacobson
  • 465
  • 1
  • 5
  • 8
  • Yep this is what I do. For my tampermonkey scripts, I just change `domain=` (e.g. https://www.google.com/s2/favicons?domain=doodstream.com) – Sridhar Sarnobat Apr 23 '22 at 22:00
14

This question is the first google search result I got when I keep searching for website favicon API. So I think it'll be still helpful in the future.

https://icon.horse/icon/[url.hostname] will give you a better site icon.

https://icon.horse/icon/stackoverflow.com

ChethiyaKD
  • 726
  • 2
  • 7
  • 13
10

You can do it without programming in 3 steps:

    1. Just open the web site, right-click and select "view source" to open the HTML code of that site. Then in the text editor search for "favicon" - it will direct you to something looking like

    <link rel="icon" href='/SOMERELATIVEPATH/favicon.ico' type="image/x-icon" />

    Take the string in href and append it to the web site's base URL (let's assume it is "http://WEBSITE/"), so it looks like

    http://WEBSITE/SOMERELATIVEPATH/favicon.ico

    which is the absolute path to the favicon. If you didn't find it this way, it can be as well in the root in which case the URL is http://WEBSITE/favicon.ico.

    2. Take the URL you determined and insert it into the href-Parameter of the following code:
    <html>
      <head>
       <title>Capture Favicon</title>   
      </head>
      <body>
        <a href='http://WEBSITE/SOMERELATIVEPATH/favicon.ico' alt="Favicon"/>Favicon</a> 
      </body>
    </html>
    
    3. Save this HTML code locally (e.g. on your desktop) as GetFavicon.html and then double-click on it to open it. It will display only a link named Favicon. Right-click on this link and select "Save target as..." to save the Favicon on your local PC - and you're done!
Matt
  • 25,467
  • 18
  • 120
  • 187
8

It's a good practice to minimize the number of requests each page needs. So if you need several icons, yandex can do a sprite of favicons in one query. Here is an example http://favicon.yandex.net/favicon/google.com/stackoverflow.com/yandex.net/

doroshko
  • 746
  • 5
  • 16
7

The first thing to look for is /favicon.ico in the site root; something like WebClient.DownloadFile() should do fine. However, you can also set the icon in metadata - for SO this is:

<link rel="shortcut icon"
   href="http://sstatic.net/stackoverflow/img/favicon.ico">

and note that alternative icons might be available; the "touch" one tends to be bigger and higher res, for example:

<link rel="apple-touch-icon"
   href="http://sstatic.net/stackoverflow/img/apple-touch-icon.png">

so you would parse that in either the HTML Agility Pack or XmlDocument (if xhtml) and use WebClient.DownloadFile()

Here's some code I've used to obtain this via the agility pack:

var favicon = "/favicon.ico";
var el=root.SelectSingleNode("/html/head/link[@rel='shortcut icon' and @href]");
if (el != null) favicon = el.Attributes["href"].Value;

Note the icon is theirs, not yours.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • 1
    Thanks Marc. I appreciate the example. Wrt the images, I don't intend to modify them or use them for anything other than icons in the context menu beside the shortcut's label. – Steven Evers Feb 25 '11 at 15:43
6

In 2020, using duckduckgo.com's service from the CLI

curl -v https://icons.duckduckgo.com/ip2/<website>.ico > favicon.ico

Example

curl -v https://icons.duckduckgo.com/ip2/www.cdc.gov.ico > favicon.ico
Alex Nolasco
  • 18,750
  • 9
  • 86
  • 81
5

You can get the favicon URL from the website's HTML.

Here is the favicon element:

<link rel="icon" type="image/png" href="/someimage.png" />

You should use a regular expression here. If no tag found, look for favicon.ico in the site root directory. If nothing found, the site does not have a favicon.

Aryan Beezadhur
  • 4,503
  • 4
  • 21
  • 42
OOO ''MMM''
  • 240
  • 3
  • 7
3
HttpWebRequest w = (HttpWebRequest)HttpWebRequest.Create("http://stackoverflow.com/favicon.ico");

w.AllowAutoRedirect = true;

HttpWebResponse r = (HttpWebResponse)w.GetResponse();

System.Drawing.Image ico;
using (Stream s = r.GetResponseStream())
{
    ico = System.Drawing.Image.FromStream(s);
}

ico.Save("favicon.ico");
Aryan Beezadhur
  • 4,503
  • 4
  • 21
  • 42
pmcilreavy
  • 3,076
  • 2
  • 28
  • 37
3

Sometimes we can't get the favicon image with the purposed solution as some websites use .png or other image extensions. Here is the working solution.

  1. Open your website with a firefox browser.
  2. Right-click on the website and click the "View page info" option from the list.
  3. It will open up a dialog and click on the "Media" tab.
  4. In that tab you will see all the images including favicon.
  5. Select the favicon.ico image or click through the images to see which image is used as favicon. Some websites use .png images as well.
  6. Then click on the "Save As" button and you should be good to go.

thanks!

Mukesh Salaria
  • 3,345
  • 1
  • 16
  • 21
1

This is a late answer, but for completeness: it is difficult to get even close to fetching 90% all favicons.

A while ago I wrote a WordPress plugin which attempts to get closer to 100%.

This is how it works:

  1. It starts by searching existing favicon repositories such as Google favicons and GetFavicons for the favicon.

  2. If none of them returns an icon, the plugin attempts to get the icon itself. This involves traversing several pages on the domain.

  3. The plugin then inspects the physical image file, because on some servers files get returned with the incorrect mime types.

The code is still not perfect because in the details you will find many weird situations: people have wrongly coded paths, e.g. img/favicon.ico where img is not in the root, duplicate headers in HTML output, different server responses from the head and body etc.

The core of the fetching part is here so you can reverse-engineer it, but be aware that validating the response should be done (checking image filetype, mime etc.).

Aryan Beezadhur
  • 4,503
  • 4
  • 21
  • 42
edelwater
  • 2,650
  • 8
  • 39
  • 67
1

The SHGetFileInfo (Check pinvoke.net for the signature) lets you retrieve a small or large icon, just as if you were dealing with a file/folder/Shell item.

Aryan Beezadhur
  • 4,503
  • 4
  • 21
  • 42
Jens
  • 77
  • 2
0

You can use Getfv.co :

To retrieve a favicon you can hotlink it at... http://g.etfv.co/[URL]

Example for this page : http://g.etfv.co/https://stackoverflow.com/questions/5119041/how-can-i-get-a-web-sites-favicon

Download content and let's go !

Edit :

Getfv.co and fvicon.com look dead. If you want I found a non free alternative : grabicon.com.

Community
  • 1
  • 1
aloisdg
  • 22,270
  • 6
  • 85
  • 105
0

http://realfavicongenerator.net/favicon_checker?site=http://stackoverflow.com gives you favicon analysis stating which favicons are present in what size. You can process the page information to see which is the best quality favicon, and append it's filename to the URL to get it.

robotik
  • 1,837
  • 1
  • 20
  • 26
0

Using jquery

var favicon = $("link[rel='shortcut icon']").attr("href") ||
              $("link[rel='icon']").attr("href") || "";
V P
  • 76
  • 7
0

Combining answers from my own research and couple of other resources below.

There are a couple of ways to achieve depending on your requirement. And some are more reliable than the others

  1. get a url to embed the favicon somewhere else:

  2. fetch multiple website favicons together

  3. other alternatives

Gangula
  • 5,193
  • 4
  • 30
  • 59