10

当我尝试使用以下代码下载一些 HTML 文件时:

$mech->get($link)
$mech->save_content("file.html");

我收到警告:

Wide character in print at C:/strawberry/perl/site/lib/WWW/Mechanize.pm line 2040.

有人可以解释我如何修复这个警告吗?

4

2 回答 2

10

您需要确保以正确的编码打开输出文件句柄。

简要浏览一下文档,看起来 Mech 并没有为保存的文件提供可配置的编码,因此您可以获取内容并自己保存:

$mech->get( $link );
my $content = $mech->content;

open my $fh, '>:utf8', $file or die "$file: $!";
print $fh $content;

中的:utf8open将确保发送到文件句柄的数据被正确编码为 UTF-8。

另一种方法是手动编码:

use Encode;
my $content = encode 'utf8', $mech->content;

open my $fh, '>', $file or die "$file: $!";
binmode $fh;
print $fh $content;
于 2011-11-27T22:21:01.757 回答
8

在版本1.73之前,您必须使用@friedo 发布的解决方案手动保存内容。

从那时起,save_content()允许您设置 Mechanize 在打开文件句柄时使用的 I/O 层。通过将 binmode 设置:utf8为如下所示,可以写入宽字符而不会发出警告:

$mech->save_content("file.html", binmode => ':utf8');
于 2014-06-29T17:49:00.557 回答