http://blog.sina.com.cn/s/blog_4acca98f0100g8u3.html
对于网络上海量的数据,如何高效的大批量自定义获取一直是像我们这些从事数据处理的必备技能之一。大多数人可能都掌握了至少一门编程语言,而这其中Perl由于其超强的文本处理能力一直受到大家的追捧。Perl非常擅长于对网页的获取和处理。之前用过一个LWP::Simple包,可以利用get()函数来获取指定路径的网页源代码,也可以对许多非文本文件进行获取和处理,比如说图片。而LWP这个包则更是强大,通过这个包,可以构建一个虚拟的浏览器,对目的网站进行抓取,或者进行相应的操作,实在是非常之强大。后面有机会一定要详细介绍下这个包!
今天新认识一个Perl包Win32::IEAutomatioin,这个包也是可以代替浏览器,进行网页查询和处理的。功能嘛,也是非常的强大,并且不像LWP包比较关注与网页的底层,所以就比较容易掌握和运用。下面我就来介绍一下这个包:
这个包的工作原理是通过调用Windows系统下的IE浏览器的DOM接口,从而利用这个接口的属性和方法,自动建立一个IE对象,所以就可以对网页进行访问和处理了。这个模块允许用户去控制Webpage,比如:像links, checkbox, buttons,radios等。此外还支持网页中的javascript处理。(强烈建议去CPAN上去看这个包的说明文档)。
下面这一段示例是来自于说明文档,相当简单,没有太多解释:
use Win32::IEAutomation;
# Creating new instance of Internet Explorer
my $ie = Win32::IEAutomation->new( visible=> 1, maximize => 1);
# Site navigation
$ie->gotoURL('http://www.google.com');
# Finding hyperlinks and clicking them
# Using 'linktext:' option (text of the link shown on webpage)
$ie->getLink('linktext:', "AboutGoogle")->Click;
# Or using 'linktext:' option with pattern matching
$ie->getLink('linktext:', qr/AboutGoogle/)->Click;
# Or using 'id:' option ( <a id=1a class=qhref=......>)
$ie->getLink('id:',"1a")->Click;
# Finding checkbox and selecting it
# Using 'name:' option ( <input type = "checkbox"name = "checkme" value = "1"> )
$ie->getCheckbox('name:',"checkme")->Select;
# Or using 'aftertext:' option (for checkbox after some text on theweb page)
$ie->getCheckbox('aftertext:', "some texthere")->Select;
# Finding text field and entering data into it
# Using 'name:' option ( <input type="text"name="username" .......> )
$ie->getTextBox('name:',"username")->SetValue($user);
# Finding button and clicking it
# using 'caption:' option
$ie->getButton('caption:', "GoogleSearch")->Click;
# Accessing controls under frame
$ie->getFrame("name:","content")->getLink("linktext:", "AllDocuments")->Click;
# Nested frames
$ie->getFrame("name:","first_frame")->getFrame("name:","nested_frame");
# Catching the popup as new window and accessing controls init
my $popup = $ie->getPopupWindow("title of popupwindow");
$popup->getButton('value:',"button_value")->Click;
这里有一点要注意,就是往往调用IE浏览器时,浏览器访问指定的网页需要一定的时间,而Perl代码则不会等待,会举行执行下一行代码,如果下一行代码是对一个新的页面进行操作的时候,就会报错!所以像调用完getButton()等转换页面的方法后,一定要调用WaitDone()方法,然后再继续下面页面的操作,不然的话就会报错:找不到这个方法。
还有一个问题就是如果你用的是傲游浏览器,设置visible=>0与visible=>1效果是一样的,都是可视化页面的。
待续。。。
补充:
安装Win32::IEAutomation的方法:ppm install Win32-IEAutomation