我正在通過使用'LWP :: UserAgent'使用以下代碼檢索'ISO-8859-1'編碼的網站。用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);
你忘由binmoding輸出手柄來設置輸出編碼爲utf8。 – tchrist