iconscraper
is a Go package that provides a robust solution to get icons from domains.
/favicon.ico
- Icon (
<link rel="icon" href="favicon.ico">
) - Web app manifest (
<link rel="manifest" href="manifest.json">
) link rel="shortcut icon"
link rel="apple-touch-icon"
link rel="msapplication-TileImage"
link rel="mask-icon"
link rel="image_src"
(also this post)meta itemprop="image"
These aren't currently scraped, but might be of interest:
import "github.com/MeVitae/iconscraper"
config := Config{
SquareOnly: true,
TargetHeight: 128,
MaxConcurrentRequests: 32,
AllowSvg: false,
}
domains := []string{"mevitae.com", "example.com", "gov.uk", "golang.org", "rust-lang.org"}
icons := iconscraper.GetIcons(config, domains)
for domain, icon := range icons {
fmt.Println("Domain: " + domain + ", Icon URL: " + icon.URL)
}
Errors related to decoding images or resources not being found on a web server (but the connection being ok) will be reported as warnings instead of errors.
By default, errors and warnings are only logged to the console. You can handle errors yourself by adding your own channel in the config, for example:
import "github.com/MeVitae/iconscraper"
config := Config{
SquareOnly: true,
TargetHeight: 128,
MaxConcurrentRequests: 32,
AllowSvg: false,
Errors: make(chan error),
}
go func(){
for err := range config.Errors {
// Handle err
}
}()
domains := []string{"mevitae.com", "example.com", "gov.uk", "golang.org", "rust-lang.org"}
icons := iconscraper.GetIcons(config, domains)
for domain, icon := range icons {
fmt.Println("Domain: " + domain + ", Icon URL: " + icon.URL)
}
Warnings can be similarly handled using the Warnings
field.
Icons can be scraped for a single domain using GetIcon
. Errors and warnings are handled in the
same way.