Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > Perl: разработка для Web > Предупреждение при парсинге страниц


Автор: FishHunter 27.2.2010, 01:52
Всем привет,

При парсинге страниц средствами LWP иногда вылетает такое предупреждение:
"Parsing of undecoded UTF-8 will give garbage when decoding entities at /usr/lib/perl5/vendor_perl/5.8.8/LWP/Protocol.pm line 114."
html подобных страниц типа получаю, но он конечно не соответствует действительности :( как это победить, подскажите плиз.

Кусок кода, который парсит:

Код

 my $browser = LWP::UserAgent->new;
    $browser->agent('Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727; .NET CLR 1.1.4322; .NET CLR 3.0.04506.30; .NET CLR 3.0.04506.648)');
    $browser->timeout(30);
    $browser->max_size(1024*1024*3);
    $browser->proxy("http","http://$proxy") if $proxy;
    $browser->cookie_jar( HTTP::Cookies->new(
             'file' => 'cookies.lwp',
             'autosave' => 1,
             ));

 my $req = HTTP::Request->new(GET=>$url);
    $req->header('Accept' => 'image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-powerpoint, application/vnd.ms-excel, application/msword, */*');
    $req->header("Accept-Language" => "en-US");
    #$req->header('Content-Type' => 'application/x-www-form-urlencoded');
    #$req->header('сonnection' => 'Keep-Alive');
    $req->header("Referer" => "$ref");
 my $resp = new HTTP::Response;
    $resp = $browser->request($req);
 my $code = $resp->code;
 my $message=$resp->message;
 my $cont = $resp->content;

Автор: dva300 4.3.2010, 07:32
Цитата

Parsing of undecoded UTF-8 will give garbage when decoding entities
    (W) The first chunk parsed appears to contain undecoded UTF-8 and one or more argspecs that decode entities are used for the callback handlers.
    The result of decoding will be a mix of encoded and decoded characters for any entities that expand to characters with code above 127. This is not a good thing.
    The solution is to use the Encode::encode_utf8() on the data before feeding it to the $p->parse(). For $p->parse_file() pass a file that has been opened in ":utf8" mode.
    The parser can process raw undecoded UTF-8 sanely if the utf8_mode is enabled or if the "attr", "@attr" or "dtext" argspecs is avoided.

более подробно http://search.cpan.org/~gaas/HTML-Parser-3.64/Parser.pm

для LWP::Simple

use LWP::Simple;
my $uri = 'htttp://somesite';
my $content = get($uri);

а для LWP::UserAgent;

use LWP::UserAgent;
my $ua = LWP::UserAgent->new();
my $response = $ua->get($uri);
my $content = $response->content();
my $data = decode_utf8($content);



Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)