HTML::...:LibXML(3pm)

πŸ’– HTML::TreeBuilder::LibXML β€” the manpage, but make it cute πŸ’–

okay so bestie, gather round, because we're talking about HTML::TreeBuilder::LibXML (that's the perl manual page, 3pm section, "User Contributed Perl Documentation" energy) and honestly? it's giving speed. πŸ¦‹


✨ NAME ✨

HTML::TreeBuilder::LibXML β€” an HTML::TreeBuilder and XPath compatible interface, but powered by libxml. periodt. πŸ‘‘


🌸 SYNOPSIS 🌸

here's the vibe, copy-paste it, no cap:

use HTML::TreeBuilder::LibXML;

my $tree = HTML::TreeBuilder::LibXML->new;
$tree->parse($html);
$tree->eof;

# $tree and $node compatible to HTML::Element
my @nodes = $tree->findvalue($xpath);
for my $node (@nodes) {
    print $node->tag;
    my %attr = $node->all_external_attr;
}

HTML::TreeBuilder::LibXML->replace_original(); # replace HTML::TreeBuilder::XPath->new

like… $tree and $node stay compatible with HTML::Element, so you get to keep your whole little XPath lifestyle πŸ’… and then replace_original() just slides in and swaps out HTML::TreeBuilder::XPath->new like it was never there. iconic behaviour.


πŸ’œ DESCRIPTION πŸ’œ

so HTML::TreeBuilder::XPath is a libxml based compatible interface to HTML::TreeBuilder, right? and listen β€” that's all fine and good, but it could be slow if your document is big. 😭 big documents + slow parsing = the worst.

enter HTML::TreeBuilder::LibXML: it's a drop-in-replacement for HTML::TreeBuilder::XPath. like, literally drop it in. 🫢

heads up tho: this module doesn't implement all of HTML::TreeBuilder and HTML::Element APIs. but bestie, enough methods are defined that modules like Web::Scraper work. so it's fine. it's fine!! ✨

πŸ”₯ BENCHMARK πŸ”₯

this is a benchmark result by tools/benchmark.pl β€” and the numbers are screaming, honestly:

Web::Scraper: 0.26
HTML::TreeBuilder::XPath: 0.09
HTML::TreeBuilder::LibXML: 0.01_01

             Rate  no_libxml use_libxml
no_libxml  5.45/s         --       -94%
use_libxml 94.3/s      1632%         --

1632% faster. the way that use_libxml said "let me be the main character" πŸ“ˆπŸ’€ that's not a glow-up, that's a transformation.


πŸŽ€ AUTHOR πŸŽ€

  • Tokuhiro Matsuno <tokuhirom@gmail.com>
  • Tatsuhiko Miyagawa <miyagawa@cpan.org>
  • Masahiro Chiba

🌈 THANKS TO 🌈

shoutout to the legends, fr fr. 🫑


⭐ SEE ALSO ⭐

  • HTML::TreeBuilder
  • HTML::TreeBuilder::XPath

go read those too, iykyk. πŸ“šβœ¨


πŸ’… LICENSE πŸ’…

this library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. slay, and legally so. βš–οΈβœ¨


and that's the whole page, bestie!! 🌸 this little gem was served up as perl v5.38.2, dated 2024-08-03, straight from the User Contributed Perl Documentation β€” so if you ever forget the details, just remember: libxml go brrr, HTML::TreeBuilder::XPath who?, and replace_original() is the plot twist. πŸ¦‹πŸ’– ily, go parse something huge and fast. πŸ‘‘

πŸ“¦ where this page comes from