嘿嘿。我之前也碰到这个问题了。
其实关键在于这一句:
my $list = get $url;
$list = encode("gbk", decode("utf8", $list));

其实在抓取内容之后,根本不要去encode, 只要decode就好了,这样就保持了perl得内码。
然后在输出得时候有很多选择,要么输出encode, 要么使用io得encoding层。

perldoc Encode



2009/11/16 liseen <[email protected]>

>
> 终端显示有点问题, 是下面这个
>
> curl http://www.hiao.com/node/hiaospace.htm | perl -ne ' s#\.\./#
> http://www\.hiao\.com/# if $. < 32; print;'  | piconv -f utf8 -t gb2312
>
>
> 2009/11/16 liseen <[email protected]>
>
>>
>> curl http://www.hiao.com/node/hiaospace.htm | perl -ne ' s#\.\./#
>> http://www\.hiao\.f $. < 32; print;'  | piconv -f utf8 -t gbk
>>
>> 2009/11/16 刺猬 <[email protected]>
>>
>> 想要抓取某一个网页,然后进行一些替换操作,生成文件后以便其他页面调用。抓取都没有问题,进行写入的时候顺便进行了正则替换和转码, 抓取的内容是
>>> utf8我要转成gb2312。然后就发现到了页面的某一行后,程序就停止了,怀疑是有特殊字符导致的,不知道应该怎么处理,有没有类似CDATA 之
>>> 类的方法呢。。
>>> 贴出代码,也请大家帮忙看看
>>> use LWP::Simple;
>>> use Encode;
>>> open TARGET, ">>target.txt";
>>> open(NEW,">union2.txt");
>>> my $url ='http://www.hiao.com/node/hiaospace.htm';
>>> my $list = get $url;
>>> $list = encode("gbk", decode("utf8", $list));
>>> if(!$list){ die "couldn't ger $url"; }
>>> else{ print NEW $list; }
>>> open(WE,"union2.txt");
>>> my $i=1;
>>> while (<WE>) {
>>>     $i++;
>>>     next if $i<32;
>>>     last if /<\/body>/;
>>>     $_=~s#\.\./#http://www\.hiao\.com/#;
>>>     next if (length($_)==1);
>>>     print    TARGET $_     ;
>>> }
>>> print    "\n";
>>> close(NEW);
>>> close(WE);
>>> close(TARGET);
>>>
>>>
>>
>
> >
>

--~--~---------~--~----~------------~-------~--~----~
您收到此信息是由于您订阅了 Google 论坛“PerlChina Mongers 讨论组”论坛。
 要在此论坛发帖,请发电子邮件到 [email protected]
 要退订此论坛,请发邮件至 [email protected]
 更多选项,请通过 http://groups.google.com/group/perlchina?hl=zh-CN 访问该论坛
-~----------~----~----~----~------~----~------~--~---

回复