2011-03-20 2 views

답변

6

필자는 BeautifulSoup을 한번도 사용하지 않았지만 설명서를 빨리 ​​보지 않고서는 HTML::TreeBuilder을 원할 수도 있습니다. 깨진 문서도 잘 처리 할 수 ​​있으며 파싱 된 트리 또는 쿼리 항목을 통과 할 수 있습니다. HTML::Elementlook_down 메서드를 확인하십시오.

XPath를/알고 있으면 daxim의 권장 사항을 참조하십시오. CSS 선택기를 통해 항목을 선택하려면 Web::Scraper 또는 Mojo::DOM을보십시오.

1

강력한 기능을 원하는 사용자는 XML :: LibXML을 사용하여 HTML을 구문 분석 할 수 있습니다. 그런 다음 이점은 XPath 및 XSLT를 포함하여 Perl이 문서를 처리 할 수있는 가장 빠르고 가장 우수한 XML 도구 체인 (MSXML 인 excecpt MSXML)을 가지고 있다는 것입니다 (XPath 및 XSLT를 포함하여 다른 언어를 사용하면 다시 구문 분석해야 함) 파서보다 XML :: 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); 
관련 문제