使用JQuery从另一个网站获取HTML:可能?法律? [英] Using JQuery to get the HTML from another website: Possible? Legal?
问题描述
我尝试从不在同一个网域的网页取得HTML程式码。 HTML文本被解析&总结了HTML代码来自该页面上的食谱(食谱名称,主要成分,步骤数量)。
然后用户可以点击链接&请访问该网域之外的网页以查看食谱。
我知道Same-Origin-Policy,但这适用于从网页获取HTML代码国外域外?我想象它与获取XML完全相同,所以这是合法&允许不是吗?
我可以从国内域外的域中获取HTML文本/代码吗? p>
使用Javascript& JQuery,这个想法是限制服务器请求的数量,通过使用户对每个配方和设备执行请求,在客户端解析HTML。这将停止服务器端瓶颈&也意味着我不必经过服务器&
$ b $ b您无法使用任何AJAX库来完成这些工作。浏览器的跨网域政策不允许您这样做。
但您可以使用php(或任何其他服务器端语言)和AJAX。创建如下的php脚本:
<?php
$ url = $ _ POST ['url'];
if($ url!=)
echo file_get_contents($ url);
?>
让我们说脚本的名字是 fetch.php
。
现在你可以从你的jQuery代码到这个 fetch.php
的AJAX调用,它会为你获取HTML代码。
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.
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?
Is there a way I can get the HTML text/code from a domain outside my domestic domain?
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.
I'm open to Solutions/Suggestions in any programming language or API or etc.
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.
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
$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.
这篇关于使用JQuery从另一个网站获取HTML:可能?法律?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!