我如何从其他网站获取元数据? [英] How I can get metadata from another site ?
问题描述
大家好。我想尝试从网站获取元标记。也许有人知道我该怎么做?
我尝试过:
我正在尝试使用这样的JS代码:
Hello everyone. I want to try get meta tags from website. Maybe someone knows how I can do this?
What I have tried:
I'm trying with JS code like this:
$.ajax({
url: "http://example.com",
dataType: 'text',
success: function(data) {
var elements = $("meta").html(data)[0].getElementsByTagName("head");
for(var i = 0; i < elements.length; i++) {
var theText = elements[i].firstChild.nodeValue;
console.log(theText);
}
}
});
但是,我收到此错误无法加载http://example.com:否'access-Control-Allow-Origin'标头出现在请求的资源上。因此,不允许来源http://example.com访问。
But, I get this error Failed to load http://example.com: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://example.com' is therefore not allowed access.
推荐答案
.ajax({
url: http://example.com,
dataType:' text',
成功: function (数据){
var elements =
.ajax({ url: "http://example.com", dataType: 'text', success: function(data) { var elements =
( meta)。html(data)[ 0 ]。getElementsByTagName( < span class =code-string> head);
for ( var i = 0 ; i< elements.length; i ++){
var theText = elements [i] .firstChild.nodeValue;
控制台跨度> .log中(theText);
}
}
});
("meta").html(data)[0].getElementsByTagName("head"); for(var i = 0; i < elements.length; i++) { var theText = elements[i].firstChild.nodeValue; console.log(theText); } } });
但是,我收到此错误无法加载http://example.com:否'access-Control-Allow-Origin'标头出现在请求的资源上。因此,不允许访问http://example.com。
But, I get this error Failed to load http://example.com: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://example.com' is therefore not allowed access.
出于安全原因,您无法在纯Javascript中执行此操作。您需要在自己的站点上使用代理脚本,该脚本向目标站点发出请求,读取元数据并将其返回到您的脚本。
如何操作取决于您使用的服务器端语言。例如,使用ASP.NET:
以编程方式读取任何页面的元标记而不在浏览器中加载 [ ^ ]
You can't do that in pure Javascript for security reasons. You'll need to use a proxy script on your own site, which issues a request to the target site, reads the metadata, and returns it to your script.
How you do that will depend on which server-side language you're using. For example, with ASP.NET:
Reading Meta Tags of Any Page Programatically without loading in browser[^]
这篇关于我如何从其他网站获取元数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!