javascript 是否可以读取另一个 url 的 dom 结构?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10691464/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it possible to read another url's dom structure?
提问by CS_2013
Obviously modifying it would be out of the question.
显然修改它是不可能的。
But you would think just reading it should not be a problem?
但是你会认为只是阅读它应该没有问题吗?
If i have my .js running on someone's system and I want to analyze the DOM of another URL , client side, is there a way to do this?
如果我在某人的系统上运行我的 .js 并且我想分析另一个 URL 的 DOM,客户端,有没有办法做到这一点?
Something simple like pull the title tag or pull the url...maybe load the site into an iframe to accomplish this?
一些简单的事情,比如拉标题标签或拉网址……也许将网站加载到 iframe 中来完成这个?
回答by Megachip
You can do this using xmlhttp
您可以使用 xmlhttp
function getSourceAsDOM(url)
{
xmlhttp=new XMLHttpRequest();
xmlhttp.open("GET",url,false);
xmlhttp.send();
parser=new DOMParser();
return parser.parseFromString(xmlhttp.responseText,"text/html");
}
回答by Jashwant
If I am getting your question right,
如果我回答对了你的问题,
A cross domain example by using yql,
使用 yql 的跨域示例,
var url = 'xyz.com'; // website you want to scrape
var yql = 'http://query.yahooapis.com/v1/public/yql?q=' + encodeURIComponent('select * from html where url="' + url + '"') + '&format=json&callback=?';
$.getJSON(yql,function(data){
if (data.results[0]){
console.log(data = data.results[0].replace(/<script[^>]*>[\s\S]*?<\/script>/gi, '')); // The scraped data (the whole webpage)
}
});
Reference: How can i get Equivalent method of HttpwebRequest in javascript
回答by Steve Binder
If the domains do not match you will not be able to do this due to a security exception. If however you control the other domain, you should research adding a cross domain file to allow access via javascript.
如果域不匹配,由于安全异常,您将无法执行此操作。但是,如果您控制另一个域,则应该研究添加跨域文件以允许通过 javascript 访问。
回答by Marduk
You could get the html source with a AJAX GET request. An then you can search in the html code or assign it to an iframe/...
您可以通过 AJAX GET 请求获取 html 源代码。然后您可以在 html 代码中搜索或将其分配给 iframe/...