WWW :: Mechanization and widespread warning of symbols

When I try to load some HTML file with the code below:

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

I get a warning:

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

Can someone explain how I can restore this warning?

+6
source share
2 answers

You need to make sure that the output file descriptors are open with the proper encoding.

With a brief look at the documents, this does not look like Mech has custom encodings for saved files, so you can capture content and save it yourself:

 $mech->get( $link ); my $content = $mech->content; open my $fh, '>:utf8', $file or die "$file: $!"; print $fh $content; 

Bit :utf8 in open ensures that data sent to the file descriptor is correctly encoded as UTF-8.

Another way to do this is to manually encode:

 use Encode; my $content = encode 'utf8', $mech->content; open my $fh, '>', $file or die "$file: $!"; binmode $fh; print $fh $content; 
+8
source

Prior to version 1.73, you had to save the contents manually using the solution sent by @friedo .

Since then, save_content() allows you to set the I / O level used by the mechanism when opening the file descriptor. By setting binmode to :utf8 as follows, wide characters are written without warning:

 $mech->save_content("file.html", binmode => ':utf8'); 
+6
source

Source: https://habr.com/ru/post/902504/


All Articles