获取多个网址的网站标题/描述
Get site titles / descriptions for multiple urls
我正在尝试获取网站图标、网站标题和外部 URL 列表的描述,最好使用 jquery。我已经成功地为我的 url 同步了 googles favicon 服务,任何人都可以阐明如何实现网站标题和描述吗?这是我到目前为止获得网站图标的内容。
HTML
<li><a href="http://dribbble.com/">Dribbble</a></li>
<li><a href="http://behance.net">Behance</a></li>
<li><a href="http://www.danwebb.net">Dan Webb</a></li>
JQUERY
$("a[href^='http']").each(function() {
$(this).prepend('<img src="https://www.google.com/s2/favicons? domain=' + this.href + '">');
});
- 您有一个额外的 space
? domain=
。
- 此外,使用
this.getAttribute("href")
或 jQuery 的 $(this).attr("href")
- 使用
https
!!!
$("a[href^='http']").each(function() {
var href= this.getAttribute("href");
$(this).prepend('<img src="https://www.google.com/s2/favicons?domain='+ href +'">';
});
<li><a href="https://dribbble.com/">Dribbble</a></li>
<li><a href="https://behance.net">Behance</a></li>
<li><a href="https://www.danwebb.net">Dan Webb</a></li>
<script src="https://code.jquery.com/jquery-3.1.0.js"></script>
- 获得称号:How can I get the title of a webpage given the url (an external url) using JQuery/JS
但是你肯定会运行进入这个错误:
...has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access
所以只用JS是不行的
PHP 救援:
由于 CORS 政策问题使用 AJAX 直接从不允许这样做的网站获取内容 - 您可以改为:
- AJAX
GET
你的服务器 上的一个 PHP 脚本,它将抓取外部站点
- 现在内容在您的域中 AJAX 将用该内容进行响应。
- 使用 JS (jQuery) 解析响应数据以获取所需的元素属性值或文本。
grabber.php
<?php
echo file_get_contents($_GET["url"]);
index.html
<ul>
<li><a href="https://dribbble.com/">Dribbble</a></li>
<li><a href="https://behance.net">Behance</a></li>
<li><a href="https://www.danwebb.net">Dan Webb</a></li>
</ul>
<script src="https://code.jquery.com/jquery-3.1.0.js"></script>
<script>
function grabber (url, el) {
$.ajax({
url: "grabber.php?url="+ url,
type: "GET",
success: function(data){
// console.log( data );
var $head = $(data).find("head");
var title = $head.find("title").text();
var desc = $head.find("meta[name='author']").attr("content");
console.log(title , desc);
$(el).append(" - "+ title +" - "+ desc);
}
});
}
$("a[href^='http']").each(function () {
var href= this.getAttribute("href");
$(this).prepend('<img src="https://www.google.com/s2/favicons?domain='+ href +'">';
grabber( href , this ); // this represent the current loop el.
});
<script>
我正在尝试获取网站图标、网站标题和外部 URL 列表的描述,最好使用 jquery。我已经成功地为我的 url 同步了 googles favicon 服务,任何人都可以阐明如何实现网站标题和描述吗?这是我到目前为止获得网站图标的内容。
HTML
<li><a href="http://dribbble.com/">Dribbble</a></li>
<li><a href="http://behance.net">Behance</a></li>
<li><a href="http://www.danwebb.net">Dan Webb</a></li>
JQUERY
$("a[href^='http']").each(function() {
$(this).prepend('<img src="https://www.google.com/s2/favicons? domain=' + this.href + '">');
});
- 您有一个额外的 space
? domain=
。 - 此外,使用
this.getAttribute("href")
或 jQuery 的$(this).attr("href")
- 使用
https
!!!
$("a[href^='http']").each(function() {
var href= this.getAttribute("href");
$(this).prepend('<img src="https://www.google.com/s2/favicons?domain='+ href +'">';
});
<li><a href="https://dribbble.com/">Dribbble</a></li>
<li><a href="https://behance.net">Behance</a></li>
<li><a href="https://www.danwebb.net">Dan Webb</a></li>
<script src="https://code.jquery.com/jquery-3.1.0.js"></script>
- 获得称号:How can I get the title of a webpage given the url (an external url) using JQuery/JS
但是你肯定会运行进入这个错误:
...has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access
所以只用JS是不行的
PHP 救援:
由于 CORS 政策问题使用 AJAX 直接从不允许这样做的网站获取内容 - 您可以改为:
- AJAX
GET
你的服务器 上的一个 PHP 脚本,它将抓取外部站点 - 现在内容在您的域中 AJAX 将用该内容进行响应。
- 使用 JS (jQuery) 解析响应数据以获取所需的元素属性值或文本。
grabber.php
<?php
echo file_get_contents($_GET["url"]);
index.html
<ul>
<li><a href="https://dribbble.com/">Dribbble</a></li>
<li><a href="https://behance.net">Behance</a></li>
<li><a href="https://www.danwebb.net">Dan Webb</a></li>
</ul>
<script src="https://code.jquery.com/jquery-3.1.0.js"></script>
<script>
function grabber (url, el) {
$.ajax({
url: "grabber.php?url="+ url,
type: "GET",
success: function(data){
// console.log( data );
var $head = $(data).find("head");
var title = $head.find("title").text();
var desc = $head.find("meta[name='author']").attr("content");
console.log(title , desc);
$(el).append(" - "+ title +" - "+ desc);
}
});
}
$("a[href^='http']").each(function () {
var href= this.getAttribute("href");
$(this).prepend('<img src="https://www.google.com/s2/favicons?domain='+ href +'">';
grabber( href , this ); // this represent the current loop el.
});
<script>