I'm trying to use HTML::TreeBuilder to parse and extract certain text from a group of HTML files. The HTML files are encoded as UTF-8. I want my output (printed to a text file) to be encoded as windows-1252.

Testing with a simple Perl script and just one input file, my output is not as I expect. Here is my code:

use HTML::TreeBuilder; $infile = "test.html"; $outfile = "test-treebuilder-output.txt"; open($fh, "<:utf8", $infile); open(OUT, ">$outfile"); binmode OUT, ':cp1252'; $tree = HTML::TreeBuilder->new(); $tree->parse_file($fh); $h1Element = $tree->look_down("_tag", "h1"); $h1TrimmedText = $h1Element->as_trimmed_text(); print OUT "$h1TrimmedText\n"; $tree->delete(); close(OUT); close($fh);

The test.html file I'm using only contains:

<html> <head> <meta http-equiv="Content-Type" content="text/html;charset=utf +-8"> <title>test</title> </head> <body> <h1>Décembre</h1> </body> </html>

My output text file contains just the word Décembre, but the file appears to be encoded as UTF-8. (Force-reading the file as ISO-8859-1 causes the accented character to display as LATIN CAPITAL LETTER A WITH TILDE followed by COPYRIGHT SIGN.)

However, I have another short test script, which doesn't use HTML::TreeBuilder, but just reads all lines from the input file and prints them to the output file. The two scripts are not materially different in how they handle the character encodings, but the output text file from this second script seems to be correctly encoded as windows-1252. Here is the second script:

$infile = "test.html"; $outfile = "test-print-output.txt"; open($fh, "<:utf8", $infile); open(OUT, ">$outfile"); binmode OUT, ':cp1252'; @infile = <$fh>; foreach $line (@infile) { print OUT $line; } close(OUT); close($fh);

I'd appreciate if someone could explain why I'm not getting the text output from my HTML::TreeBuilder script correctly encoded as windows-1252.


In reply to HTML::TreeBuilder, UTF-8 input, windows-1252 output by osullic

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.