当我尝试使用LWP :: Useragent和Encode进行字符编码从网页中撤回全局地址时,我在perl中有编码问题.我试过谷歌搜索解决方案,但似乎没有任何工作.我正在使用Strawberry Perl 5.12.3.
以美国驻捷克共和国大使馆的地址页为例(http://prague.usembassy.gov/contact.html).我想要的只是撤回地址:
地址:Tržiště15118 01 Praha 1 - MaláStrana捷克共和国
哪个firefox使用字符编码UTF-8正确显示,UTF-8与网页标题字符集相同.但是当我尝试使用perl将其拉回并将其写入文件时,尽管在Useragent或Encode :: decode中使用了decoding_content,编码看起来仍然搞砸了.
我已经尝试在数据上使用正则表达式来检查错误是不是在打印数据时(即内部在perl中正确)但错误似乎在于perl如何处理编码.
这是我的代码:
#!/usr/bin/perl
require Encode;
require LWP::UserAgent;
use utf8;
my $ua = LWP::UserAgent->new;
$ua->timeout(30);
$ua->env_proxy;
my $output_file;
$output_file = "C:/Documents and Settings/ian/Desktop/utf8test.txt";
open (OUTPUTFILE, ">$output_file") or die("Could not open output file $output_file: $!" );
binmode OUTPUTFILE, ":utf8";
binmode STDOUT, ":utf8";
# US embassy in Czech Republic webpage
$url = "http://prague.usembassy.gov/contact.html";
$ua_response = $ua->get($url);
if (!$ua_response->is_success) { die "Couldn't get data from $url";}
print 'CONTENT TYPE: …Run Code Online (Sandbox Code Playgroud)