fix: recover pages wrapped in a single form, and remove extraction artifacts - #898
Open
cvl01 wants to merge 2 commits into
Open
fix: recover pages wrapped in a single form, and remove extraction artifacts#898cvl01 wants to merge 2 commits into
cvl01 wants to merge 2 commits into
Conversation
…tifacts Extraction returned an unrelated sidebar caption instead of the article on alsumaria.tv, an Arabic news site, and precision mode returned only that caption. Four independent causes, each reproducible on its own: - tree_cleaning() deletes every <form>. ASP.NET WebForms puts the entire page inside one <form id="aspnetForm">, so the whole document was discarded and only chrome outside the form was left to extract. A form holding most of the remaining text is a layout wrapper and is now demoted to a plain container, while widget forms (search, login, newsletter) are still removed. Forms are handled after the other noise is gone so that script and head text cannot skew the comparison. - PRECISION_DISCARD_XPATH matched 'bottom' as a bare substring, so it also matched CSS spacing utilities. The article column carries 'Padding-bottom-lg-30', which made precision mode discard the article itself. 'bottom' now has to start or end a class token, the same tightening already applied to 'link'. - handle_other_elements() emits a text-bearing div whole, and appending it moves it into result_body -- but _extract() iterates a subelems list captured beforehand, so an <lb> inside that div was visited again and re-emitted its tail. That duplicated a paragraph. Elements already moved into result_body are now skipped; handle_paragraphs() marks the children it consumes "done", and this covers the ones it cannot retag. - handle_paragraphs() treated whitespace-only text as content, and stripping divs merges their indentation into the preceding <lb>'s tail, so pruned ad slots and clearfix spacers surfaced as blank, space-filled lines. Both are now dropped, leaving code and pre untouched, where that whitespace is the indentation itself. MANUALLY_CLEANED keeps "form" so the documented list still reflects that forms are removed, and an in-place removal by a user is honoured rather than raising. No change on the evaluation corpus for default, fast and recall modes; precision mode gains one true negative (F1 0.9088 -> 0.9104). Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #898 +/- ##
==========================================
- Coverage 99.68% 99.66% -0.03%
==========================================
Files 21 21
Lines 4110 4132 +22
==========================================
+ Hits 4097 4118 +21
- Misses 13 14 +1 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
On alsumaria.tv, an Arabic news site, extraction returns an unrelated sidebar video caption instead of the article. In precision mode that caption is the only output:
Tracing it turned up four independent causes. Each reproduces on its own, and two are not site-specific — any page with
<br>-separated text in adivand an inline ad block hits them.Causes and fixes
1. A single wrapping
<form>discards the whole document.tree_cleaning()deletes every<form>. ASP.NET WebForms puts the entire page inside one<form id="aspnetForm">, so nothing was left but the chrome outside it, which is what got extracted.A form holding most of the remaining text is a layout wrapper, not a widget, and is now demoted to a plain container. Genuine widget forms (search, login, newsletter) hold little text and are still removed. Forms are handled after the other noise is gone, so
<script>and<head>text cannot skew the comparison.MANUALLY_CLEANEDkeeps"form", so the documented list still reflects that forms are removed. Since that list is public API users mutate in place (docs/settings.rst, #746), an in-place removal of"form"is now honoured rather than raising.2.
'bottom'matched CSS spacing utilities.PRECISION_DISCARD_XPATHmatchedbottomas a bare substring. The article column carriesPadding-bottom-lg-30, so precision mode discarded the article itself.bottomnow has to start or end a class token — the same tightening already applied to'link'. Page-bottom chrome (bottom,bottom-bar,article-bottom) is still discarded;Padding-bottom-lg-30andborder-bottom-0are not.3. A paragraph was emitted twice.
handle_other_elements()emits a text-bearingdivwhole, and appending it moves it intoresult_body— but_extract()iterates asubelemslist captured beforehand, so an<lb>inside that div was visited again and re-emitted its tail as a second paragraph. Elements already moved intoresult_bodyare now skipped.handle_paragraphs()marks the children it consumes"done"; this covers the ones it cannot retag.4. Blank, space-filled lines.
handle_paragraphs()treated whitespace-only text as content (plain truthiness), and stripping divs merges their indentation into the preceding<lb>'s tail — so pruned ad slots and clearfix spacers surfaced as lines of spaces. Both are now dropped, leavingcodeandpreuntouched, where that whitespace is the indentation itself.Result
Title, author, date and categories also come out correctly with
--with-metadata.Testing
masterand pass with the fix:test_precision_discard_bottom_token_boundary,test_wrapper_form_kept,test_no_duplicate_paragraph_from_lb_tail. The form test also covers theMANUALLY_CLEANEDin-place override.ruffandmypyclean.tests/evaluate.py: default, fast and recall modes are unchanged; precision mode gains one true negative.I used Claude Opus for coding