我正在尝试用Perl做一个简单的网络爬虫,但是很多网站都有动态内容加载,例如,使用javascript函数:
$(document).ready(function() {
$("#blabla").load('blublu/bla.php');
});因此,我正在尝试调整我已经拥有的网络爬虫(获取HTML内容),以“等待”这些脚本加载,然后才获取整个(和完整)网站内容(HTML)。
到目前为止,我发现有人说这可以通过WWW::Mechanize,Mechanize::Mozilla,WWW::Mechanize::Firefox来实现。
问题是,我对Perl编程和模块实现不是很在行,所以我想知道是否有好心的人愿意在这里发布一个简单的示例或教程来说明我所要求的如何实现!
发布于 2013-01-15 17:37:00
使用www::mechanize::firefox,你必须从Firefox 'addon store‘安装和配置mozrepl插件。
对于起点,有几个示例程序可以用作起点:http://search.cpan.org/dist/WWW-Mechanize-Firefox/lib/WWW/Mechanize/Firefox/Examples.pm
此页面包含一个如何等待特定HTML元素的示例:http://search.cpan.org/dist/WWW-Mechanize-Firefox/lib/WWW/Mechanize/Firefox/Cookbook.pod#Wait_until_an_element_appears
它可以很容易地定制:
# It will be wait 10 seconds for blabla, then timeout
my $retries = 10;
while ($retries-- and ! $mech->is_visible( xpath => '//*[@id="blabla"]' )) {
sleep 1;
};
die "Timeout" if 0 > $retries;
# Now the element exists
$mech->click({xpath => '//*[@id="submit"]'});https://stackoverflow.com/questions/14331687
复制相似问题