1

我见过很多关于使用 Jquery + node.js + YQL 抓取 html 的问题。它没有提到从网页获取 css 和 javascript。

有没有办法在不使用服务器端技术的情况下获取外部网站的 html、css 和 javascript?

*我需要在代码中发生这种情况,以便我可以在 webapp 中使用结果。

4

1 回答 1

0

可能与此重复。你的答案就在这里。

你应该看看 jQuery。它具有丰富的 AJAX 功能基础,可以让您完成所有这些工作。您可以加载外部页面,并使用类似 CSS 的直观选择器解析其 HTML 内容。

使用示例$.get();

$.get("anotherPage.html", {}, function(results){
  alert(results); // will show the HTML from anotherPage.html
  alert($(results).find("div.scores").html()); // show "scores" div in results
});

对于外部域,我不得不编写一个充当中间人的本地 PHP 脚本。jQuery 将调用本地 PHP 脚本,传入另一个服务器的 URL 作为参数,本地 PHP 脚本将收集数据,jQuery 将从本地 PHP 脚本中读取数据。

$.get("middleman.php", {"site":"http://www.google.com"}, function(results){
  alert(results); // middleman gives Google's HTML to jQuery
});

注意: PHP 的东西适用于不同的域。

于 2013-02-12T05:07:52.217 回答