Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Parsing of undecoded UTF-8 will give garbage when decoding entities

by itsscott (Sexton)
on Aug 25, 2015 at 00:24 UTC ( [id://1139753]=perlquestion: print w/replies, xml ) Need Help??

itsscott has asked for the wisdom of the Perl Monks concerning the following question:

Greetings Wise Monks

Over the years I have created and maintained a crawler for a customer. As of late a few sites that they crawl various Parser modules (pullparser/tokeparser) have been getting noisy. (warnings) with this noise "Parsing of undecoded UTF-8 will give garbage when decoding entities"

I have done all the searches and tried all the solutions that I have found from simply using utf8_mode(1) to encoding/decoding the page before I pass it to the parser to no avail.

I have saved the pages to disk to examine them and they seem fine. We are using curl::multi to get the pages, and all is well for all but a couple sites. These sites are using a CDN as they are quite large, though, this is the norm for a number of their clients and we don't have the same warnings

I'm not even sure how to ask how to fix what I don't know is broken. I suppose the right question is what can someone suggest to help me track down the WHY it's being noisy, when the pages seem to be utf8 encoded properly. The utf8 flag is on ie.

my $pagetoparse = HTML::TokeParser->new(doc => \$pagein); $pagetoparse->utf8_mode(1);

$page in is the raw html document, it appears in all testing to be properly encoded and not mixed etc etc.

Is it something simple that I have overlooked or misunderstood, and why is it not on every page? it's generated by a cms, but it barks only so many x pages....

Thank you in advance for any wisdom that is rendered. I've run out of ideas.

I have looked here, tried everything, searched on google, tried every possible solution I could understand to try (must be one that I have missed, or not understood)

Scott

  • Comment on Parsing of undecoded UTF-8 will give garbage when decoding entities
  • Download Code

Replies are listed 'Best First'.
Re: Parsing of undecoded UTF-8 will give garbage when decoding entities
by aitap (Curate) on Aug 25, 2015 at 07:52 UTC

    $page in is the raw html document
    Does $page consist of bytes (e.g. "\x{e2}\x{98}\x{ba}", which can be decoded as U+263A White Smiling Face in UTF-8, or "\x{fe}\x{ff}\x{26}\x{3a}" which is the same U+263A, but in UTF-16), or of characters (e.g. "\x{263A}", which is a U+263A White Smiling Face character and should be encoded before writing it anywhere)? HTML::TokeParser seems to ask for the latter (it wants HTML to be decoded to characters from bytes in whatever encoding they were encoded to). See also: perlunitut.

    Of course, this brings us to another problem of correctly determining the encoding of a byte stream, which sometimes should be done by an HTML parser (when charset is defined by meta tag in HTML4/HTML5), sometimes should be done by HTTP client (when a proper Content-type header is sent) and sometimes just has to be guessed. And it's not impossible to misconfigure a webserver to serve Content-Type: text/html; charset=utf-8 with <meta charset="koi8-r"> in HTML while the real encoding is UTF-16LE with BOM.

      Honestly, from what I've seen no. The only 'extended' characters are a few 'smart' apostrophes and a copyright symbol. I determined this with bbedit by opening the raw file, and switching the encoding to latin1 from the utf-8. each page is pretty much identical, but that's a good point about the headers. I will investigate that next. It's so hard figuring this kind issue as it's the module barking and our code!

      Thanks for the input so far!

        Only ASCII characters (with ord <= 0x7f) are represented in UTF-8 in the same way as in latin1 (as single bytes). By the way, there is a module IO::HTML which can be used to determine encoding of HTML files (seekable :raw streams only).

        If you are positive that your web pages consist only of ASCII and valid UTF8, you can use HTML::TokeParser::->new( \ decode "UTF-8", $raw_html ); (or even utf8::decode($html); HTML::TokeParser::->new($html)), but it's going to complain and/or produce mojibake (or at least U+FFFD REPLACEMENT CHARACTERs) if (when?) the crawler encounters latin1/cp1252/koi8/another non-ASCII encoding.

Re: Parsing of undecoded UTF-8 will give garbage when decoding entities ( HTML::Parser utf8_mode )
by Anonymous Monk on Aug 25, 2015 at 04:21 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1139753]
Approved by Old_Gray_Bear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2024-04-25 10:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found