打开网页并使用JavaScript解析它 [英] Open webpage and parse it using JavaScript

查看:132
本文介绍了打开网页并使用JavaScript解析它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道JavaScript可以在新窗口中打开链接,但是可以打开网页而无需在窗口中打开它或将其显示给用户吗?我想要做的是解析该网页的某些文本并将其用作变量。

I know JavaScript can open a link in a new window but is it possible to open a webpage without opening it in a window or displaying it to the user? What I want to do is parse that webpage for some text and use it as variables.

这可能没有来自服务器端语言的任何帮助吗?如果是这样,请向我发送我可以实现此目的的方向。

Is this possible without any help from server side languages? If so, please send me in a direction I can achieve this.

全部谢谢

推荐答案

您可以使用 XMLHttpRequest 对象来执行此操作。这是一个简单的例子

You can use an XMLHttpRequest object to do this. Here's a simple example

var req = new XMLHttpRequest();  
req.open('GET', 'http://www.mydomain.com/', false);   
req.send(null);  
if(req.status == 200)  
   dump(req.responseText);

加载后,您可以使用 javascript正则表达式

Once loaded, you can perform your parsing/scraping by using javascript regular expressions on the req.responseText member.

在实践中,您需要做更多的工作才能获得XMLHttpRequest对象跨平台方式,例如:

In practice you need to do a little more to get the XMLHttpRequest object in a cross platform manner, e.g.:

var ua = navigator.userAgent.toLowerCase();
if (!window.ActiveXObject)
  req = new XMLHttpRequest();
else if (ua.indexOf('msie 5') == -1)
  req = new ActiveXObject("Msxml2.XMLHTTP");
else
  req = new ActiveXObject("Microsoft.XMLHTTP");



或者使用图书馆......



或者,您可以节省所有麻烦,只需使用像 jQuery 原型为您解决此问题。

Or use a library...

Alternatively, you can save yourself all the bother and just use a library like jQuery or Prototype to take care of this for you.

请注意,由于同源策略,您请求的页面必须与发出请求的页面位于同一个域中。如果你想要一个远程页面,你必须通过服务器端脚本代理它。

Note that due to the same-origin policy, the page you request must be from the same domain as the page making the request. If you want to request a remote page, you will have to proxy that via a server side script.

另一种可能的解决方法是使用Flash来发出请求,如果目标站点使用适当配置的crossdomain.xml文件授予权限,则允许跨域请求。

Another possible workaround is to use Flash to make the request, which does allow cross-domain requests if the target site grants permission with a suitably configured crossdomain.xml file.

这是一篇关于同源策略主题的好文章:

Here's a nice article on the subject of the same-origin policy:

  • Same-Origin Policy Part 1: Why we’re stuck with things like XSS and XSRF/CSRF

这篇关于打开网页并使用JavaScript解析它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆