读书人

网页抓取之新方法 (在java程序中施用j

发布时间: 2012-09-09 09:27:54 作者: rapoo

网页抓取之新方法 (在java程序中使用jQuery)

??? 你想要的任何信息,基本上在互联网上存在了,问题是如何把它们整理成你所需要的,比如在某个行业网站上抓取所有相关公司的的名字,联系电话,Email等,然后存到Excel里面做分析。网页信息抓取变得原来越有用了。


??? 一般传统的网页,web服务器直接返回Html,这类网页很好抓,不管是用何种方式,只要得到html页面,然后做Dom解析就可以了。但对于需要Javascript生成的网页,就不那么容易了。张瑜目前也没有找到好办法解决此问题。各位有抓javascript网页经验的朋友,欢迎指点。


??? 所以今天要谈的还是传统html网页的信息抓取。虽然前面说了,没有技术难度,但是是否能有相对更容易的方法呢? 用过jQuery等js框架的朋友,可能都会觉得javascript貌似抓取网页信息的天然助手,而且其出生就是为了网页解析而存在的。当然现在有更多的应用了,如Server端的javascript应用,NodeJs.


??? 如果能在我们的应用程序,如java程序中,能使用jQuery去抓网页,绝对是件激动人心的事情 。确实有现成的解决方案,一个Javascript引擎,一个能支撑jQuery运行的环境就可以了。


??? 工具: java, Rhino, envJs. 其中 Rhino是Mozzila提供的开源Javascript引擎,envJs是一个模拟浏览器额环境,如Window等。 代码如下,

?

?

?? 测试代码:

?

$.ajax({  url: "http://www.baidu.com",  context: document.body,  success: function(data){ //   util.log(data);        var result =parseHtml(data);        var $v= jQuery(result); //   util.log(result);    $v.find('#u a').each(function(index) {         util.log(index + ': ' + $(this).attr("href"));  //        arr.add($(this).attr("href"));    });  }}); function parseHtml(html) {       //Create an iFrame object that will be used to render the HTML in order to get the DOM objects        //created - this is a far quicker way of achieving the HTML to DOM conversion than trying        //to transform the HTML objects one-by-one         var oIframe = document.createElement('iframe');     //Hide the iFrame from view         oIframe.style.display = 'none';         if (document.body)            document.body.appendChild(oIframe);        else            document.documentElement.appendChild(oIframe);                //Open the iFrame DOM object and write in our HTML        oIframe.contentDocument.open();        oIframe.contentDocument.write(html);        oIframe.contentDocument.close();            //Return the document body object containing the HTML that was just        //added to the iFrame as DOM objects        var oBody = oIframe.contentDocument.body;            //TODO: Remove the iFrame object created to cleanup the DOM            return oBody;    }

?

?

??? 我们执行Unit Test,将会在控制台打印从网页上抓取的三个baidu的连接,

0: http://www.baidu.com/gaoji/preferences.html
1: http://passport.baidu.com/?login&tpl=mn
2: https://passport.baidu.com/?reg&tpl=mn

?

?? 测试成功,故证明在java程序中用jQuery抓取网页是可行的.

?

----------------------------------

张瑜,Mybeautiful, zhangyu0182@sina.com

?

推荐阅读,

Java学习这七年??? 如何阅读源代码??? 我应该做的更差吗? 1 楼 Mybeautiful 2012-03-09 补充一下,
经过研究,如果Rhino能结合jsdom那将能解决javascript的问题,就如同node.js一样。有相关经验的朋友,提示一下。

读书人网 >编程

热点推荐