Dear Sisters and Brothers in Perl!

I'm trying to download some webpages containing non-ASCII characters. The web-page declares it to be

<meta http-equiv="Content-Type" content="text/html; charset=windows-12 +51" />
and in my browser (FireFox 3) it shows very nice.

Now I have written a little script to get me one page as follows:

use strict; use LWP::UserAgent; my @captions; my $response = $ua->get('http://www.1418.ru/chronicles.php?p=100'); if ($response->is_success) { my $file = $response->content; $file =~ m/<h3>(.*)<\/h3>/i; my $h3_content = $1; push @captions, $h3_content; } else { warn 'ERROR: no HTML ',$response->status_line; }
Before one of you starts saying that I should not parse HTML with a regex: I know and besides there is only one <h3>-tag on the page, so it seemed a bit overkill to break out HTML::Parser or HTML::TreeBuilder

After I have downloaded all pages I need, I save @captions into a file and when I open the file (which is actually an HTML-file with the proper charset declaration) it does no longer show Cyrillic charcaters, but funny accented characters.

So I thought that I needed to use a Cyrillic encoding as follows:

open my $fh, '>:encoding(iso-8859-5)', 'c:/data/captions2.txt'; print $fh join '\n', @captions;
But that gives me a lot of errors such as:
"\x{00ff}" does not map to iso-8859-5.
(iso-8859-5 is the iso name for windows-1251) and the file is full of these "\x{00ff}" character encodings and still does not render correctly.

So I guess that things already go wrong when importing the web-page and that somewhere there the proper encoding gets lost and cannot be restored.

I think my question really boils down to "how to convince LWP::UserAgent to keep the Cyrillic endcoding of the webpage?"

Update: LWP::UserAgent indeed did not decode the webpage and all was solved when it put the proper encoding (windows-1251) in the HTML header. Thanks all!

CountZero

A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James


In reply to Downloading webpages with non-ASCII characters by CountZero

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.