2011-03-20 20 views

cevap

6

Hiçbir zaman BeautifulSoup kullanmamıştım, ancak belgelerindeki hızlı kaymadan HTML::TreeBuilder'u isteyebilirsiniz. Bozuk belgeleri bile iyi işleyebilir ve ayrıştırılmış ağaç veya sorgu öğelerine göre geçişe izin verir - HTML::Element'da look_down yöntemine bakın.

Eğer XPath'ı seviyorsanız/biliyorsanız, daxim'in tavsiyelerine bakın. CSS seçiciden öğeler seçmek isterseniz, Web::Scraper veya 'a bakın.

1

Güç arıyorsanız, XML'yi ayrıştırmak için XML :: LibXML kullanabilirsiniz. Bunun avantajı, en hızlı ve en iyi XML araç zincirinin tüm gücüne sahip olmanızdır (yalnızca MS olan MSXML'yi), belgenizi işlemek için Perl'e (XPath ve XSLT dahil) sahip olmanızdır (eğer başka bir tane kullanırsanız yeniden ayrıştırmayı gerektirir) XML'den ayrıştırıcı :: LibXML).

use strict; 
use warnings; 
use XML::LibXML; 
# In 1.70, the recover and suppress_warnings options won't shup up the 
# warnings. Hence, a workaround is needed to keep the messages away from 
# the screen. 
sub shutup_stderr { 
    my($subref, $bufref) = @_; 
    open my $fhbuf, '>', $bufref; 
    local *STDERR = $fhbuf; 
    $subref->(); # execute code that needs to be shut up 
    return; 
} 
# ==== main ============================================================ 
my $url = shift || 'http://www.google.de'; 
my $parser = XML::LibXML->new(recover => 2); # suppress_warnings => 1 
# Note that "recover" and "suppress_warnings" might not work - see above. 
# https://rt.cpan.org/Public/Bug/Display.html?id=58024 
my $dom; # receive document 
shutup_stderr 
    sub { $dom = $parser->load_html(location => $url) }, # code 
    \my $errmsg; # buffer 
# Now process document as XML. 
my @nodes = $dom->getElementsByLocalName('title'); 
printf "Document title: %s\n", $_->textContent for @nodes; 
printf "Lenght of error messages: %u\n", length $errmsg; 
print '-' x 72, "\n"; 
print $dom->toString(1);