perl - 使用 perl 和 Firefox::Marionette 下载文件

标签 perl

尝试使用 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 来获取真实窗口,并且脚本因等待打开/保存对话框的确认而挂起,如下图所示:

enter image description here

单击“确定”后,文件即被下载。

问题是,如何绕过确认对话框,以便能够在 headless 模式下运行脚本而无需手动单击。

此外,欢迎使用任何其他方法从上述站点下载文件,它位于 cloudflare 后面,所以我未能使用一些基本的 LWP

最佳答案

您可以通过设置文件的 mime_types 来绕过下载弹出窗口(有关更多信息,请参阅 this 答案)。使用您提供的 MIME 类型 application/x-amz-json-1.0,以下内容适用于 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;

关于perl - 使用 perl 和 Firefox::Marionette 下载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61184476/

相关文章:

perl - 学习 Perl - 哪个版本?

perl - 仅当 Perl 中存在时才有效获取哈希条目

perl - 在perl中解析制表符分隔的文件

regex - Perl 多行正则表达式语句

perl - 是否可以在 Perl 6 中实现 lisp "language"?

php - 我不明白这个 Textile Regex

python - 正则表达式只匹配各种括号外的内容

macos - latexindent:自 macOS 11 Big Sur 以来安装 File::HomeDir、Mac::SystemDirectory 时遇到问题

perl - 如何在 Perl 中将数组数据插入 MySQL?

regex - 为什么这种否定的后视被认为是成功的正则表达式匹配?