用perl访问带有图片验证码的后台

有时候好奇的想,如果用perl去访问一个带有图片验证码登录的用户中心,怎么用perl登录呢?先是想到 获取图片验证码,然后用orc 技术去破解,但是想到像discuz这样的验证码肯定识别率极低。后来在网上google 了一下,发现解决思路是这样的

先访问网站,找到图片验证码的路径,然后把图片下载下来(注意要同一个浏览器进程)保存成一个图片,然后人工识别这个图片,再手工输入即可了,这种方法,可以破解99% 的图片验证码

网上找来的参考代码如下

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");

来源

http://stackoverflow.com/questions/18289338/how-to-use-perl-to-download-a-file-which-need-a-code-confirm