pQuery is a pragmatic port of the jQuery JavaScript framework to Perl which can be used for screen scraping.
pQuery quite sensitive to malformed HTML. Consider the following example:
use pQuery;
my $html_malformed = "<html><head><title>foo</title></head><body>bar</body></html>>";
my $page = pQuery($html_malformed);
my $title = $page->find("title");
print "The title is: ", $title->html, "\n";
pQuery won't find the title tag in the example above due to the double ">>
" in the malformed HTML.
To make my pQuery based applications more tolerant to malformed HTML I need to pre-process the HTML by cleaning it up before passing it to pQuery.
Starting with the code fragment given above, what is the most robust pure-perl way to clean-up the HTML to make it parse:able by pQuery?
I'd report this as a bug in pQuery. Here's a workaround:
use HTML::TreeBuilder;
use pQuery;
my $html_malformed = "<html><head><title>foo</title></head><body>bar</body></html>>";
my $html_cleaned = HTML::TreeBuilder->new_from_content($html_malformed);
my $page = pQuery($html_cleaned->as_HTML);
$html_cleaned->delete;
my $title = $page->find("title");
print "The title is: ", $title->html, "\n";
This doesn't make a lot of sense, since pQuery already uses HTML::TreeBuilder as its underlying parsing mechanism, but it does work.
Try HTML::Tidy
, which fixes invalid HTML.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With