尝试使用perl和Firefox::Marionette自动进行某些网站抓取,尤其是文件下载。
这是简短的示例代码(用于文件下载)。
#!/usr/bin/env perl
use 5.014;
use warnings;
use Firefox::Marionette();
use Path::Tiny;
my $ff = Firefox::Marionette->new();
#my $ff = Firefox::Marionette->new(
# visible => 1
#);
#my $dwl = 'https://www.curseforge.com/wow/addons/dazaralor-totems/download/2610166/file'; # direct download link (not work correctly)
my $dwl = 'https://www.curseforge.com/wow/addons/dazaralor-totems/download/2610166'; # download link, leading to redirect page
$ff->go($dwl);
while(!$ff->downloads()) { sleep 1 }
while($ff->downloading()) { sleep 1 }
foreach my $p ($ff->downloads()) {
say $p;
path($p)->copy('./toto.zip');
}
$ff->quit;
运行脚本,它将挂起。因此,尝试使用visible => 1
来获得真实的窗口,并且脚本挂起,因为等待确认打开/保存对话框的消息如下图所示:
单击“确定”后,将下载文件。
问题是,如何绕过确认对话框,以便无需手动单击即可以无头模式运行脚本。
此外,欢迎使用任何其他方法从上述站点下载文件,因为它位于cloudflare后面,所以我无法使用一些基本的LWP
。
答案 0 :(得分:2)
您可以通过为文件设置mime_types
来绕过下载弹出窗口(有关更多信息,请参见this答案)。使用您提供的application/x-amz-json-1.0
MIME类型,以下内容在Ubuntu 19.10上对我有效:
use feature qw(say);
use strict;
use warnings;
use Path::Tiny;
use Firefox::Marionette ();
use Firefox::Marionette::Capabilities;
my $ff = Firefox::Marionette->new(
mime_types => ['application/x-amz-json-1.0'],
visible => 0,
capabilities => Firefox::Marionette::Capabilities->new(
page_load_strategy => 'none'
)
);
my $dwl = 'https://www.curseforge.com/wow/addons/dazaralor-totems/download/2610166/file';
$ff->go($dwl);
while(!$ff->downloads()) { say "No downloads yet.."; sleep 1 }
while($ff->downloading()) { say "Downloading.."; sleep 1 }
foreach my $p ($ff->downloads()) {
path($p)->copy('./toto.zip');
}
$ff->quit;