如何检索网站的图标?

时间:2010-01-02 03:09:32

标签: web favicon

我想在我的网站上列出精选网站,我认为尊重和使用他们的网站很酷。如何从域中获取JSP或XSLT中的任意URL?我可以启动PHP或javascript,但XSLT是首选的方法。

6 个答案:

答案 0 :(得分:57)

答案 1 :(得分:24)

要获取网站的图标,您需要加载每个特色网站的索引HTML并检查以下任一项:

HTML:

<link rel="icon" type="image/vnd.microsoft.icon" href="http://example.com/image.ico">
<link rel="icon" type="image/png" href="http://example.com/image.png">
<link rel="icon" type="image/gif" href="http://example.com/image.gif">

XHTML:

<link rel="icon" type="image/vnd.microsoft.icon" href="/somepath/image.ico" />
<link rel="icon" type="image/png" href="/somepath/image.png" />
<link rel="icon" type="image/gif" href="/somepath/image.gif" />

Internet Explorer可能使用稍微不同的格式:

<link rel="SHORTCUT ICON" href="http://www.example.com/myicon.ico" />

另请注意,由于大多数Web浏览器不需要HTML链接来检索图标,因此如果找不到上述链接引用,还应检查网站文档根目录中的favicon.ico

使用PHP,可以使用file_get_contents($url)轻松获取网页的HTML内容:

$url = 'http://www.exmaple.com';
$output = file_get_contents($url);

答案 2 :(得分:1)

这是我的尝试。它使用各种策略来解决许多可能的情况:

<?
/*
  nws-favicon : Get site's favicon using various strategies

  This script is part of NWS
  https://github.com/xaccrocheur/nws/

*/


function CheckImageExists($imgUrl) {
    if (@GetImageSize($imgUrl)) {
        return true;
    } else {
        return false;
    };
};

function getFavicon ($url) {

$fallback_favicon = "/var/www/favicon.ico";    
// $fallback_favicon = "http://stackoverflow.com/favicon.ico";


    $dom = new DOMDocument();
    @$dom->loadHTML($url);
    $links = $dom->getElementsByTagName('link');
    $l = $links->length;
    $favicon = "/favicon.ico";
    for( $i=0; $i<$l; $i++) {
        $item = $links->item($i);
        if( strcasecmp($item->getAttribute("rel"),"shortcut icon") === 0) {
            $favicon = $item->getAttribute("href");
            break;
        }
    }

    $u = parse_url($url);

    $subs = explode( '.', $u['host']);
    $domain = $subs[count($subs) -2].'.'.$subs[count($subs) -1];

    $file = "http://".$domain."/favicon.ico";
    $file_headers = @get_headers($file);

    if($file_headers[0] == 'HTTP/1.1 404 Not Found' || $file_headers[0] == 'HTTP/1.1 404 NOT FOUND' || $file_headers[0] == 'HTTP/1.1 301 Moved Permanently') {

        $fileContent = @file_get_contents("http://".$domain);

        $dom = @DOMDocument::loadHTML($fileContent);
        $xpath = new DOMXpath($dom);

        $elements = $xpath->query("head/link//@href");

        $hrefs = array();

        foreach ($elements as $link) {
            $hrefs[] = $link->value;
        }

        $found_favicon = array();
        foreach ( $hrefs as $key => $value ) {
            if( substr_count($value, 'favicon.ico') > 0 ) {
                $found_favicon[] = $value;
                $icon_key = $key;
            }
        }

        $found_http = array();
        foreach ( $found_favicon as $key => $value ) {
            if( substr_count($value, 'http') > 0 ) {
                $found_http[] = $value;
                $favicon = $hrefs[$icon_key];
                $method = "xpath";
            } else {
                $favicon = $domain.$hrefs[$icon_key];
                if (substr($favicon, 0, 4) != 'http') {
                    $favicon = 'http://' . $favicon;
                    $method = "xpath+http";
                }
            }
        }

        if (isset($favicon)) {
            if (!CheckImageExists($favicon)) {
                $favicon = $fallback_favicon;
                $method = "fallback";
            }
        } else {
            $favicon = $fallback_favicon;
            $method = "fallback";
        }

    } else {
        $favicon = $file;
        $method = "classic";

        if (!CheckImageExists($file)) {
            $favicon = $fallback_favicon;
            $method = "fallback";
        }

    }
    return $favicon;
}

?>

答案 3 :(得分:0)

对于Firefox,您可以使用https://addons.mozilla.org/en-US/firefox/addon/httpfox/。加载网站,然后按F10&gt; ...&gt; “在自己的窗口中打开HttpFox”然后查找“image / x-icon”;在右侧的列中是URL。

答案 4 :(得分:0)

打开页面源代码(右键单击View page source)找到下面提到的行,单击images / favicon.png链接。

<link rel="icon" href="images/favicon.png" type="image/png" sizes="16x16">

答案 5 :(得分:0)

  • 使用IE,将该站点添加为书签

  • 将快捷方式从书签菜单拖到桌面上

  • 使用(真实)文本编辑器打开生成的.URL

  • 文件中的 IconFile 行将指向 到Web服务器上的收藏夹文件中

  • 浏览到文件...中提琴!