【发布时间】:2024-01-20 17:14:01
【问题描述】:
我想用 ajax 或 jquery 阅读 www.google.com 的 html 源代码(我不只是想显示源代码,我需要解析它,所以有 xmlhttp.responseText 很好)。
read contents of an external webpage and get specific elements 有一个很好的方式在服务器端使用 php Can Javascript read the source of any web page? 很好,如果你想读取本地域的页面
yql+JSON 是一种可能,如上所述,但看起来很慢且开销很大
我更喜欢 ajax,因为我不需要加载 90k 的 jquery 库,而且据我所知......
var xmlhttp=null;
var url = 'bot.html?url=http://google.com'; //must redirect in bot.html
//var url='http://www.google.com'; wont work, 0 xmlhttp.status error
if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest(); //src says buggy for IE7
} else {// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
document.getElementById("result").innerHTML= xmlhttp.responseText;
}
}
和jquery差不多...
$("#result").load(url);
在其他提到的 * 中未提及的是如何处理 ?url= 。我做了(保留所有 js)...
bot.html:
<head>
<script type="text/javascript">
var vars = query.split("&");
var pair = vars[0].split("=");
if (pair[0]=='url') { // ex bot.html?url=http://www.google.com
alert('hi '+pair[1]);
window.location = pair[1];
//top.location.href=pair[1]; or
}
</script>
... above jquery or ajax ...
<div id="result">Fill Me</div>
所有这些都适用于本地页面 var url='index.php' (没有重定向),但是,这些都不适用于外部链接,如 google.com,我似乎无法 var url='google 。因为它正在加载,而不是在做。我想我可以对 ajax 使用相同的代理技巧。
尝试通过 .htaccess 重定向/代理不适合此应用程序
【问题讨论】:
标签: ajax proxy jquery-load http-redirect