WWW::Mechanize 出现了一些问题。
我正在尝试加载的页面包含 Javascript,它等待 30 秒,然后解析我需要单击的按钮(以确认我已同意站点条款:))
我知道 Mechanize 不适用于 JS,因此我需要使用 cookie 获取此页面,当我按下此按钮时,此站点会放入我的浏览器。
这些cookies是这样的:同意=>1,上次访问=>1388753990,上次访问=>0
我怎样才能使用这些“机载”cookie 获得所需的页面?
代码:
my $cookie_jar = HTTP::Cookies->new;
my $agent = WWW::Mechanize->new( cookie_jar => $cookie_jar );
$cookie_jar->set_cookie("agreed"=>1,"lastseen"=>0,"lastvisit"=>1388753990);
$agent->get( 'http://www.example.com' );
print $agent->content();
感谢帮助!
最佳答案
如果你想要真正的 Mechanize 移动到 WWW-Mechanize-Firefox
然后你会得到真正的 JS 环境:
use WWW::Mechanize::Firefox;
my $mech = WWW::Mechanize::Firefox->new();
$mech->get('http://ursite.com');
# The submit button is generated after the page has loaded
my $retries = 10;
while ($retries-- and ! $mech->is_visible( xpath => '//*[@id="submit"]' )) {
sleep 1;
};
die "Timeout" if 0 > $retries;
# Now the element exists
$mech->click({xpath => '//*[@id="submit"]'});
关于Perl WWW::Mechanize 和 cookies ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20905121/