如何检索网站图标?
我正在使用 Ruby on Rails v3.0.9,我想检索我设置链接的每个网站的 favicon.ico
图像。
也就是说,如果在我的应用程序中我设置了 http://www.facebook.com/
URL,我想检索 Facebook 的图标并在我的网页中使用\插入该图标。当然,我也想为所有其他网站这样做。
如何以“自动”方式从网站检索 favicon.ico
图标(“自动”是指在网站中搜索 favicon 并获取链接到它 - 我认为没有,因为并非所有网站都有一个名为“favicon.ico”的图标,我想以“自动”方式识别它)?
PS:我想做的是像 Facebook 在您的 Facebook 页面中添加链接\URL 时所做的那样:它识别相关的网站徽标,然后将其附加到链接\URL 中。
I am using Ruby on Rails v3.0.9 and I would like to retrieve the favicon.ico
image of each web site for which I set a link.
That is, if in my application I set the http://www.facebook.com/
URL I would like to retrieve the Facebook' icon and use\insert that in my web pages. Of course I would like to do that also for all other web sites.
How can I retrieve favicon.ico
icons from web sites in an "automatic" way (with "automatic" I mean to search for a favicon in a web site and get the link to it - I think no because not all web sites have a favicon named exactly 'favicon.ico'. I would like to recognize that in an "automatic" way)?
P.S.: What I would like to make is something like Facebook makes when to add a link\URL in your Facebook page: it recognizes the related web site logo and then appends that to the link\URL.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
http://getfavicon.appspot.com/ 非常适合获取网站图标。只需提供网站的网址,您就会得到网站图标:
http: //g.etfv.co/http://www.google.com
http://getfavicon.appspot.com/ works great for fetching favicons. Just give it the url for the site and you'll get the favicon back:
http://g.etfv.co/http://www.google.com
最近我写了一些类似的解决方案。
如果我们想要找到 favicon url,它不仅可以是
.ico
文件,也可以不在根目录中,我们应该解析目标站点 html。在 Ruby on Rails 中,我使用 nokogiri gem 进行 html 解析。
首先,我们解析所有
itemprop
属性包含image
关键字的元标记。在目标站点使用 https://schema.org/WebPage 模板的情况下,需要更现代的技术不仅仅是link
标签。如果我们找到它,我们可以使用
content
属性作为 favicon url。但为了确定,我们应该检查它是否真的存在 URL。如果我们找不到一些元标记,那么我们会搜索标准的
link
标记,其中rel
属性包含icon
关键字。这是 W3C 标准情况 (https://www.w3.org/2005/10/ howto-favicon)以及我的解决方案的一些代码:
Recently I have written some similar solution.
If we want find favicon url, that can be not only
.ico
file and can be not in the root, we should parse target site html.In Ruby on Rails, I have used nokogiri gem for html parsing.
First we parse all meta tags where
itemprop
attribute containsimage
keyword. It is necessary in situations where target site used https://schema.org/WebPage template, that more modern technology than justlink
tag.If we found it, we can use
content
attribute as favicon url. But we should check it for really URL existence, just to be sure.If we can't found some meta tags, then we search for standard
link
tags, whererel
attribute containsicon
keyword. This is W3C standard situation (https://www.w3.org/2005/10/howto-favicon)And some code of my solution:
可以通过两种方式找到网站图标。首先,有一个“硬编码”的传统名称“http://example.com/favicon.ico”。
其次,HTML 页面可以通过
和其他一些方式在其
部分中定义网站图标。 (您可能想阅读有关 favicon 的维基百科文章)
因此,您的自动机可能会获取给定网站的主页,对其进行解析并检查是否有正确的
标记,然后作为后备,尝试“硬编码”
favicon.ico
名称。The favicons are being found by two ways. First, there is a 'hardcoded', traditional name of `http://example.com/favicon.ico'.
Second, the HTML pages may define the favicon in their
<head>
sections, by<link rel="icon"...>
and a few other. (You may want to read the Wikipedia article about favicon)So, your automat may fetch the main page of given website, parse it and check whether there are proper
<link>
tags, and then, as a fallback, try the "hardcoded"favicon.ico
name.我想我错过了你的问题...
你想从另一个网站获取一个网站图标并将其设为你的吗?
如果这就是您想要的,您可以直接从主页图标获取并将其保存在您的公共文件夹中。
因此:
www.facebook.com
favicon:www.facebook.com/favicon.ico
拍摄该图像并以名称 favicon 保存在您的公共文件夹中
,完成后应该就足够了
如果你想要动态的,你可以使用jquery,但如果你想要静态的,你可以放置一个图像标签指向:[网站的根url]/favicon.ico,
如下所示:
<%= image_tag "#{website.url}/favicon.ico" %>
I think I missed your question ...
you want to grab a favicon from another site and make it yours?
if that's what you want, you can get directly from the home icon and save it in your public folder.
thus:
www.facebook.com
favicon:www.facebook.com/favicon.ico
take that image and save with the name favicon in your public folder
done it should be sufficient
if you want it dinamicaly you can use jquery, but if you want that static you can put a image tag pointing to: [root url of the website]/favicon.ico
like this:
<%= image_tag "#{website.url}/favicon.ico" %>
使用 javascript (jQuery),如下所示: http://jsfiddle.net/aX8T4/
With javascript (jQuery), like this: http://jsfiddle.net/aX8T4/
难道不能只使用常规的 img 标签,并将 src 属性指向 favicon 吗?
这假设浏览器将 .ico 文件识别为图像。帮助方法可能也适用于此。
Can't you just use a regular img tag with the src attribute pointing to the favicon?
This assumes a browser recognizes a .ico file as an image. Helped methods would probably work with this too.
您可以使用 pismo gem 轻松完成此操作。
获取 Facebook 图标 url 的简单示例:
You can do it easily with pismo gem.
Quick example to get the url of Facebook's favicon:
这是我的 ruby 方法,它将去掉 URL 的末尾,附加图标,并生成图像标签。
Here's my ruby method, that will strip the end off a URL, append the favicon, and produce an image tag.