I am having trouble with HTML::TreeBuilder and utf-8 encoding/decoding, particularly with the ’ character.

Here is a stripped down version of my code:

UPDATE:code modified to correct syntax error.

require HTTP::Request; require LWP::UserAgent; use HTML::Entities; use HTML::TreeBuilder; use Encode; $ua = LWP::UserAgent->new; # Get the page $request = HTTP::Request->new("GET", "http://buyingguide.winemag.com/c +atalog/peju-1998-reserve-cabernet-sauvignon-napa-rutherford"); $response = $ua->request($request); $body = $response->content(); #dump the file open (DMP, ">", "dumpfile.html"); print DMP $body; close DMP; #parse it $root = HTML::TreeBuilder->new; $root->parse($body); $root->eof; $review_et = $root->look_down('itemprop','reviewBody'); print $review_et->as_HTML . "\n"; $review = $review_et->as_text; print $review . "\n";

When I view the webpage in a browser it contains the string, "many ’98 Cabs". That same string shows up in the source for the page, so its not encoded in the source. The string shows up in the dumpfile.html as well. But after parsing in HTML::TreeBuilder, as_HTML prints it as "many ’98 Cabs" and as_text prints it as "many Γاض98 Cabs".

Setting or unsetting utf8_mode doesn't solve the problem, actually, setting seems to exacerbate it. Ive tried explicitly setting STDOUT to utf-8 via binmode, but that doesn't help either. encode_utf8 or decode_utf8 before printing is also no help. Ive seen several other questions here about TreeBuilder and utf-8 similar to mine, but the solutions there have not appeared to solve my problem.

What am I not getting?


In reply to TreeBuilder and encoding by VineMob

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.