java 有没有办法以编程方式从网站 URL 中获取徽标图标?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10456663/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 01:07:08  来源:igfitidea点击:

Any way to grab a logo icon from website URL, programmatically?

javaandroidxmllayoutwebview

提问by mask

I am in the process of creating an activity where I will show the list of website visited with its logo and its alias name chosen by user.

我正在创建一个活动,我将在其中显示访问过的网站列表,其中包含用户选择的徽标和别名。

e.g.

例如

  • Recent Websites Visited

    logo1 website1/alias name

    logo2 website2/alias name

    .

    . so no

  • 最近访问的网站

    logo1 网站1/别名

    logo2 网站2/别名

    .

    . 所以不行

The question is, (Ref. attached image) How to get website logo displayed on left side of http://?

问题是,(参考附图)如何让网站标志显示在 http:// 的左侧?

Like below I want to just grab the icon and save it locally

如下所示,我只想抓取图标并将其保存在本地

回答by Hyman Fairfield

Use this website:

使用本网站:

https://besticon-demo.herokuapp.com/allicons.json?url=www.stackoverflow.com

https://besticon-demo.herokuapp.com/allicons.json?url=www.stackoverflow.com

It will find all logos for a website in multiple sizes and return a nice json string with meta data including the url to the icon. You simply replace www.stackoverflow.comwith your domain.

它将找到多种尺寸的网站的所有徽标,并返回一个带有元数据的漂亮 json 字符串,包括图标的 url。您只需替换www.stackoverflow.com为您的域。

The site also has a gui for entering in websites manually if you prefer:

如果您愿意,该站点还有一个用于手动输入网站的 gui:

https://besticon-demo.herokuapp.com/

Here is a sample string returned from querying for the stack overflow website:

这是查询堆栈溢出网站返回的示例字符串:

{
   "url":"www.stackoverflow.com",
   "icons":[
      {
         "url":"http://stackoverflow.com/apple-touch-icon.png",
         "width":158,
         "height":158,
         "format":"png",
         "bytes":3445,
         "error":null,
         "sha1sum":"c78bd457575a3221c6b3d0d17ffb00ffc63d7cd0"
      },
      {
         "url":"http://cdn.sstatic.net/Sites/stackoverflow/img/favicon.ico?v=4f32ecc8f43d",
         "width":32,
         "height":32,
         "format":"ico",
         "bytes":5430,
         "error":null,
         "sha1sum":"4f32ecc8f43d0986b9c6ce9f37999e86c0b829ef"
      },
      {
         "url":"http://stackoverflow.com/favicon.ico",
         "width":32,
         "height":32,
         "format":"ico",
         "bytes":5430,
         "error":null,
         "sha1sum":"4f32ecc8f43d0986b9c6ce9f37999e86c0b829ef"
      }
   ]
}

回答by Ry-

It's called a favicon, and all you have to do is:

它被称为收藏夹图标,您所要做的就是:

  1. If there's an icon at /favicon.ico, use that.
  2. Otherwise, get the content of the page, and extract the location from <link rel="shortcut icon" href="URL goes here" />. You'll need to use an HTML parser and find the <link>with a relof either iconor shortcut icon.
  1. 如果 处有图标/favicon.ico,请使用它。
  2. 否则,获取页面内容,并从 中提取位置。你需要使用一个HTML解析器,并找到了任或。<link rel="shortcut icon" href="URL goes here" /><link>reliconshortcut icon

回答by Kannan o v

Try using this code:

尝试使用此代码:

imageview1.setImageBitmap(webview1.getFavicon());

回答by Akshay Paliwal

This method can be used to get Favicon Icon bitmap

此方法可用于获取 Favicon Icon 位图

 private Bitmap fetchFavicon(Uri uri) {
        final Uri iconUri = uri.buildUpon().path("favicon.ico").build();
        Log.i(TAG, "Fetching favicon from: " + iconUri);

        InputStream is = null;
        BufferedInputStream bis = null;
        try
        {
            URLConnection conn = new URL(iconUri.toString()).openConnection();
            conn.connect();
            is = conn.getInputStream();
            bis = new BufferedInputStream(is, 8192);
            return BitmapFactory.decodeStream(bis);
        } catch (IOException e) {
            Log.w(TAG, "Failed to fetch favicon from " + iconUri, e);
            return null;
        }
    }

回答by dcollien

Here's a python library which tries to infer the logo image from a URL:

这是一个尝试从 URL 推断徽标图像的 python 库:

https://github.com/dcollien/urlimage

https://github.com/dcollien/urlimage

it parses the HTML at the url, and tries a whole bunch of things including:

它解析 url 处的 HTML,并尝试很多事情,包括:

  • meta tag with itemprop="image" or property="image"
  • meta tag with property="og:image:secure_url" or property="og:image"
  • meta tag with name="twitter:image"
  • meta tag for Microsoft tiles, with: name="msapplication-wide310x150logo", name="msapplication-square310x310logo", name="msapplication-square150x150logo", name="msapplication-square70x70logo"
  • link tag with rel="apple-touch-icon"
  • link tag with rel="icon"
  • tries out "{scheme}://{domain}/favicon.ico" to see if it exists
  • otherwise pulls out the first img tag (next to an h1)
  • 带有 itemprop="image" 或 property="image" 的元标记
  • 带有 property="og:image:secure_url" 或 property="og:image" 的元标记
  • 名称为 =“twitter:image”的元标记
  • Microsoft 磁贴的元标记,包括:name="msapplication-wide310x150logo", name="msapplication-square310x310logo", name="msapplication-square150x150logo", name="msapplication-square70x70logo"
  • 带有 rel="apple-touch-icon" 的链接标签
  • 带有 rel="icon" 的链接标签
  • 尝试“{scheme}://{domain}/favicon.ico”以查看它是否存在
  • 否则拉出第一个 img 标签(在 h1 旁边)

回答by Kunal Gupta

Use this logo.clearbit.com/stackoverflow.com

使用此徽标.clearbit.com/stackoverflow.com

You can even customize to get particular size and grascaled version

您甚至可以自定义以获得特定尺寸和分级版本

logo.clearbit.com/stackoverflow.com?size=80&greyscale=true

logo.clearbit.com/stackoverflow.com?size=80&greyscale=true