我想在我的网站上列出特色网站,我认为尊重和使用他们的网站图标会很酷。如何从域中获取 JSP 或 XSLT 中的任意 URL?我可以关闭 PHP 或 javascript,但 XSLT 是首选方法。
问问题
57876 次
6 回答
81
于 2011-04-25T08:35:13.940 回答
29
要获取网站的图标,您需要加载每个特色网站的索引 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,很容易通过以下方式获取网页的 HTML 内容file_get_contents($url)
:
$url = 'http://www.exmaple.com';
$output = file_get_contents($url);
于 2010-01-02T03:14:11.127 回答
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;
}
?>
于 2013-09-11T18:51:26.770 回答
0
对于 Firefox,您可以使用https://addons.mozilla.org/en-US/firefox/addon/httpfox/。加载一个网站,然后按 F10 > ... >“在自己的窗口中打开 HttpFox”,然后查找“image/x-icon”;右侧的列中是 URL。
于 2017-06-26T09:49:50.800 回答
0
打开页面源代码(右键单击查看页面源代码)找到下面提到的行,单击 images/favicon.png 链接。
<link rel="icon" href="images/favicon.png" type="image/png" sizes="16x16">
于 2017-08-16T11:14:46.593 回答
0
使用 IE,为网站添加书签
将快捷方式从书签菜单拖到桌面上
使用(真实)文本编辑器打开生成的 .URL
文件中有一行IconFile,它将指向 Web 服务器上的 favicon 文件
浏览到文件...中提琴!
于 2019-04-02T23:29:40.113 回答