我如何解析使用纯Java脚本的远程HTML页面 [英] How can i parse remote html page using pure java script

查看:65
本文介绍了我如何解析使用纯Java脚本的远程HTML页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要求解析远程html页面(例如:www.mywesite.com/home)我怎样才能得到这个网站的HTML页面源码,我如何解析这个页面



html是这样的

 < html> 
< body>
< div class =my-class1>
< a href =home / link?id = 1> hello< / a>
< / div>

< div class =my-class1>
< a href =home / link?id = 2>嘿< / a>
< / div>

< div class =my-class1>
< a href =home / link?id = 3> bye< / a>
< / div>
< / body>
< / html>

我想输出为

  hello 
hey
bye

我是不使用任何服务器端技术(如java,.net)我只想用java脚本实现这个功能



是否可以使用纯javaScript或任何其他语言解析远程html页面其他jQuery插件

预先感谢

解决方案

访问除自身以外的任何服务器的远程页面内容。



您可以:


  1. 服务器获取远程内容

  2. 在远程服务器的配合下,您可以使用适当的CORS( http://en.wikipedia.org/wiki/Cross-origin_resource_sharing )安排。


  3. 再次通过与远程服务器的合作,如果通过javascript使其内容可用,您可以通过创建内联脚本元素来访问它。 JSONP就是这种方法的一个例子。

  4. 如果您编写浏览器插件或插件 - 对于允许使用javascript编写此类内容的浏览器,您不会以同样的方式受到浏览器安全模式的约束。


I have a requirement to Parse remote html page ( ex: www.mywesite.com/home) how can i get this website html page source and how can i parse this page

that html is like this

 <html>
     <body>
        <div class="my-class1">
             <a href="home/link?id=1">hello</a>
        </div>

        <div class="my-class1">
             <a href="home/link?id=2">hey</a>
        </div>

        <div class="my-class1">
             <a href="home/link?id=3">bye</a>
        </div>
     </body>
 </html>

i want output as

 hello
 hey
 bye 

I'm not using any server side technology(like java, .net) i want to achieve this using java script only

is it possible to parse remote html page using Pure javaScript or any other jQuery plugin

thanks in advance

解决方案

Ordinary browser javascript cannot access the contents of remote pages from any server except its own.

You can:

  1. Have a cooperating script on your own server to fetch the remote content

  2. With the cooperation of the remote server, you may be able to access content with an appropriate CORS ( http://en.wikipedia.org/wiki/Cross-origin_resource_sharing ) arrangement.

  3. Again with the cooperation of the remote server, if it makes its content available by javascript you can access that by creating inline script elements. "JSONP" is an example of this approach.

  4. If you write a browser plugin or addon - for browsers which permit such things to be written in javascript - then you are not bound by the browser security model in the same way.

这篇关于我如何解析使用纯Java脚本的远程HTML页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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