0

我刚开始学习一些基于 Perl 的 Web 应用程序,但是,我遇到了一个问题,我尝试编写一个 Perl 脚本来下载文件,在下载之前需要输入代码。例如,请查看此网址:http ://epaper.dfdaily.com/dfzb/page/1/2013-08/17/A01/20130817A01_pdf.pdf 。

我试图以某种方式谷歌搜索我选择使用 WWW::Mechanize,如下代码,但我无法获取文件。任何人都可以帮助我吗?非常感谢!!

我的代码在这里:(我想已经得到正确的代码并将其存储到 $code):

my $mech = WWW::Mechanize -> new();
$mech -> get($url);
$mech -> submit_form(
         form_number => 0,
         fields => {checkCode => $code}
     );

print $mech -> content;
4

1 回答 1

0

这是演示如何执行此操作的示例代码。该代码将在程序目录中创建captcha.jpg文件,因此您可以检查它并在此之后输入 CAPTCHA:

use strict;
use warnings;
use FindBin qw($Bin);
#use HTML::TreeBuilder::XPath;
use WWW::Mechanize;

my $mech = WWW::Mechanize->new();
$mech->agent_alias("Windows IE 6");
$mech->get(
    "http://epaper.dfdaily.com/dfzb/page/1/2013-08/17/A01/20130817A01_pdf.pdf");

#you don't need commented code
#because CAPTCHA URL is always the same for this site
#my $tree = HTML::TreeBuilder->new_from_content( $mech->content() );
#my ($src) = $tree->findvalues('//img[@id="checkcode"]');
$mech->get("http://203.156.244.168:9000/validatecodegen");
open my $fh, ">:raw", "$Bin/captcha.jpg" or die $!;
print {$fh} $mech->content();
close $fh;
$mech->back();

print "Input CAPTCHA: ";
my $code = <>;
chomp $code;
$mech->submit_form(

    with_fields => {
        checkCode => $code,

    },
    button => "Submit",
);

$mech->save_content("$Bin/result.pdf");
于 2013-08-17T14:39:31.303 回答