Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/90.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何阅读网页以获取其html内容_Javascript_Html - Fatal编程技术网

Javascript 如何阅读网页以获取其html内容

Javascript 如何阅读网页以获取其html内容,javascript,html,Javascript,Html,我想得到一个网页的html内容进行处理。然后使用该网页中的关键词在谷歌上搜索。请告诉我如何获取以下示例的html代码: <html> <head> <script> //Here I would like to read news.yahoo.com page and get the html content of that page for further processing var a = window.open("https://www.goo

我想得到一个网页的html内容进行处理。然后使用该网页中的关键词在谷歌上搜索。请告诉我如何获取以下示例的html代码:

<html>
<head>
<script>

  //Here I would like to read news.yahoo.com page and get the html content of that page for further processing

var a = window.open("https://www.google.com/#q=keyword1");  // search keyword in google
setTimeout(function() { a.close() }, 100);

var b = window.open("https://www.google.com/#q=keyword2");  // search keyword in google
setTimeout(function() { b.close() }, 100);

</script>
</head>
<body>
</body>
</html>

//在这里,我想阅读news.yahoo.com页面并获取该页面的html内容,以便进一步处理
变量a=窗口打开(“https://www.google.com/#q=keyword1");  // 谷歌搜索关键词
setTimeout(函数(){a.close()},100);
变量b=窗口打开(“https://www.google.com/#q=keyword2");  // 谷歌搜索关键词
setTimeout(函数(){b.close()},100);

您通常希望在服务器上进行某种后台工作。如果您在客户端执行此操作,您的用户将必须等待您的网页加载,然后等待后续每个网页加载(在您的示例中,在windows
a
b
),然后等待您处理这些脚本,等等


根据所使用的服务器端语言,您可以使用各种不同的库获取页面的html。根据您的问题历史,我假设您使用的是PHP;请参阅(及其答案)作为示例。

正如zzzBov所指出的,如果要使用javascript,需要将node.js与PhantomJS或CasperJS结合使用

PhantomJS和CasperJS是无头浏览器,允许您使用javascript选择器来抓取网站,并对其执行任何操作


您还可以使用PHP、Python、Ruby或任何您精通的编程语言,但您需要使用服务器端语言

解决此类问题的最优雅的解决方案是使用服务器端web服务检索和处理您的网页数据(来自news.yahoo.com等),并使用XML或JSON将其呈现给客户端


研究

使用JavaScript执行跨域HTTP请求的唯一方法是使用有权发出请求的JavaScript环境。通常是服务器端JavaScript,比如Node,但理论上,您可以从域内页面上运行的bookmarklet调用JavaScript。