Javascript 使用 JQuery 从另一个网站获取 HTML:可能吗?合法的?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8501127/
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
Using JQuery to get the HTML from another website: Possible? Legal?
提问by sazr
I am trying to get HTML code from a webpage that is not in the same domain. The html text is parsed & summarises a recipe(recipe name, main ingredients, no. of steps) found on that page the HTML code was from.
我正在尝试从不在同一域中的网页获取 HTML 代码。html 文本被解析并总结了在 HTML 代码来自的页面上找到的食谱(食谱名称、主要成分、步骤数)。
The user can then click the link & go to that webpage outside the domain to view the recipe.
然后,用户可以单击该链接并转到域外的该网页以查看食谱。
I'm aware of the Same-Origin-Policy, but does that apply to getting HTML code from a webpage outside the domestic domain? I imagine its exactly the same as getting XML, so this is legal & allowed isn't it?
我知道同源策略,但这是否适用于从国内域外的网页获取 HTML 代码?我想它与获取 XML 完全相同,所以这是合法的并且允许的,不是吗?
Is there a way I can get the HTML text/code from a domain outside my domestic domain?
有没有办法从我国内域之外的域中获取 HTML 文本/代码?
Using Javascript & JQuery, the idea is to limit the amount of server requests & storage by having the user perform requests for each recipe & parsing the HTML on the client side. This stops server side bottlenecks & also means I dont have to go through the server & delete old outdated recipe summarisations.
使用 Javascript 和 JQuery,其想法是通过让用户对每个配方执行请求并在客户端解析 HTML 来限制服务器请求和存储的数量。这停止了服务器端的瓶颈,也意味着我不必通过服务器并删除旧的过时的配方摘要。
I'm open to Solutions/Suggestions in any programming language or API or etc.
我对任何编程语言或 API 等的解决方案/建议持开放态度。
采纳答案by Nishchay Sharma
What you are trying to do can't be done using any AJAX library. Browsers' cross-domain policy won't allow you to do this.
使用任何 AJAX 库都无法完成您尝试执行的操作。浏览器的跨域策略不允许您这样做。
But you can do this with a combination of php (or any other server-side language) and AJAX. Create a php script like this:
但是您可以结合使用 php(或任何其他服务器端语言)和 AJAX 来做到这一点。像这样创建一个 php 脚本:
<?php
$url=$_POST['url'];
if($url!="")
echo file_get_contents($url);
?>
Let us say the script's name is fetch.php
.
Now you can throw an AJAX call from your jQuery code to this fetch.php
and it will fetch the HTML code for you.
假设脚本的名称是fetch.php
. 现在你可以从你的 jQuery 代码中抛出一个 AJAX 调用fetch.php
,它会为你获取 HTML 代码。
回答by AlienWebguy
No, this will not work from client-side JavaScript. The browser prevents it for security reasons. You would need to make ajax calls to a local server-side script (PHP, for example) which would then fetch the content (via cURL, for example) and return the HTML you want.
不,这不适用于客户端 JavaScript。浏览器出于安全原因阻止它。您需要对本地服务器端脚本(例如 PHP)进行 ajax 调用,然后该脚本将获取内容(例如,通过 cURL)并返回您想要的 HTML。
回答by Matteo Mosca
To add something to the answers you already got, I can tell you that html
is not meant to be used as a way to transmit data "like a service". For that purpose there is XML
or JSON
exposed through SOAP
or REST
.
为了给你已经得到的答案添加一些东西,我可以告诉你,这html
并不意味着用作“像服务一样”传输数据的方式。为此目的,有XML
或JSON
通过SOAP
或暴露REST
。
In your scenario, the best approach that I can think of, keeping in mind both technical and legal aspects, is to use an iframe
to display the external content and citing the source of the iframe content, including an external link like you're already doing.
在您的场景中,我能想到的最佳方法,同时牢记技术和法律方面,是使用 aniframe
来显示外部内容并引用 iframe 内容的来源,包括您已经在做的外部链接.
You can still try the server side approach to fetch the remote html but again, not a clean way to do it, surely not a good practice and possibly not legal.
您仍然可以尝试使用服务器端方法来获取远程 html,但同样,这不是一种干净的方法,肯定不是一个好习惯,也可能不合法。
If the author of the content wants it to be reusable outside of its site, he can express this intent by making the unformatted content available through a service or an RSS
/ Atom
feed.
如果内容的作者希望它在其站点之外可重用,他可以通过服务或RSS
/Atom
提要提供未格式化的内容来表达此意图。
回答by OnesimusUnbound
The same origin applies. try this code and you'll face security error
同样的起源也适用。试试这个代码,你会面临安全错误
$.get("other web page site", {}, function(content){
$("#receipe").html(content)
}, "html")
btw, you'll more likely violate copyright law, so be wary ;-)
顺便说一句,你更有可能违反版权法,所以要小心 ;-)
回答by voigtan
I′m not to sure if it counts as pure javascript solution but: http://developer.yahoo.com/yql/could help you with what you are looking for.
我不确定它是否算作纯 javascript 解决方案,但是:http: //developer.yahoo.com/yql/可以帮助你找到你正在寻找的东西。