我正在使用带有以下代码的“LWP::UserAgent”检索“ISO-8859-1”编码的网站。
问题是特殊字符显示不正确,尤其是“€”符号显示错误。
内容编码被识别为“ISO-8859-1”,这是正确的。
为了显示检索到的文本,我将它保存到一个文件中并使用 Notepag++ 打开它。
问题:如何以正确的方式检索“ISO-8859-1”编码的特殊字符?
#SENDING REQUEST
my $ua = LWP::UserAgent->new();
$ua->agent('Mozilla/5.0 (Windows NT 6.1; WOW64; rv:15.0) Gecko/20100101 Firefox/15.0.1'); # pretend we are very capable browser
my $req = HTTP::Request->new(GET => $url);
#add some header fields
$req->header('Accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8');
$req->header('Accept-Language', 'en;q=0.5');
$req->header('Connection', 'keep-alive');
$req->header('Host', 'www.url.com');
#SEND
my $response = $ua->request($req);
#decode trial1
print $response->content_charset(); # gives ISO-8859-1 which is right
my $content = $response->decoded_content(); #special chars are displayed wrong
#decode trial2
my $decContent = decode('ISO-8859-1', $response->content());
my $utf8Content = encode( 'utf-8', $decContent ); #special char € is displayed as Â
#decode trial3
Encode::from_to($content, 'iso-8859-1', 'utf8'); #special char € is displayed as  too
#example on writing data to file
open(MYOUTFILE, ">>D:\\encodingperl.html"); #open for write, overwrite
print MYOUTFILE "$utf8Content"; #write text
close(MYOUTFILE);