Official Crawl4AI Cloud: a hosted web scraping, extraction, and search API for AI agents. Free API key, 1,000 credits a month, no card. Fully open source: this repo is the Apache-2.0 crawler that powers it, and you can self-host it.
Python aiai-agentscrawl4ai-cloudcrawlerdata-extractionllmmarkdownmcpopen-sourceplaywrightpythonragscraperweb-crawlerweb-crawlingweb-scraping
Fix: keep page content nested in an unclosed <noscript>
Thomas Yu6b5a0d4608da
diff --git a/crawl4ai/content_scraping_strategy.py b/crawl4ai/content_scraping_strategy.pyindex 67e87250..8b8b07e1 100644--- a/crawl4ai/content_scraping_strategy.py+++ b/crawl4ai/content_scraping_strategy.py@@ -36,6 +36,28 @@ import copyOG_REGEX = re.compile(r"^og:")TWITTER_REGEX = re.compile(r"^twitter:")DIMENSION_REGEX = re.compile(r"(\d+)(\D*)")+NOSCRIPT_OPEN_REGEX = re.compile(r"<noscript\b[^>]*>", re.IGNORECASE)+NOSCRIPT_CLOSE_REGEX = re.compile(r"</noscript\s*>", re.IGNORECASE)+++def unwrap_orphaned_noscript(html: str) -> str:+ """Drop <noscript> tags, keeping their contents, when one is left unclosed.++ Pages that nest <noscript> (lazy-load plugins commonly wrap Google Tag+ Manager's own noscript) lose a closing tag when a scripting-enabled browser+ serializes them: the browser ends the element at the first </noscript> and+ discards the stray one. lxml parses with scripting disabled, so it nests the+ rest of the document inside the still-open element, and the noscript+ removal below then deletes the whole page.++ Unbalanced tag counts identify that serialization damage, so well-formed+ documents are returned untouched.+ """+ opens = len(NOSCRIPT_OPEN_REGEX.findall(html))+ closes = len(NOSCRIPT_CLOSE_REGEX.findall(html))+ if opens <= closes:+ return html+ return NOSCRIPT_CLOSE_REGEX.sub("", NOSCRIPT_OPEN_REGEX.sub("", html))# Function to parse srcset@@ -626,7 +648,7 @@ class LXMLWebScrapingStrategy(ContentScrapingStrategy):success = Truetry:- doc = lhtml.document_fromstring(html)+ doc = lhtml.document_fromstring(unwrap_orphaned_noscript(html))# Match BeautifulSoup's behavior of using body or full doc# body = doc.xpath('//body')[0] if doc.xpath('//body') else docbody = docdiff --git a/tests/test_unclosed_noscript.py b/tests/test_unclosed_noscript.pynew file mode 100644index 00000000..f39c90d7--- /dev/null+++ b/tests/test_unclosed_noscript.py@@ -0,0 +1,69 @@+"""+Tests for pages whose body ends up nested inside an unclosed <noscript>.++A page that nests <noscript> (for example a lazy-load plugin wrapping Google Tag+Manager's own noscript) loses its outer closing tag when a scripting-enabled+browser serializes it. Re-parsed by lxml, the rest of the document becomes a+child of the still-open <noscript>, and the scraper's noscript removal used to+delete the entire page, leaving cleaned_html with an empty body.+"""++import pytest++from crawl4ai.content_scraping_strategy import (+ LXMLWebScrapingStrategy,+ unwrap_orphaned_noscript,+)++# Shape of page.content() for such a page: two opening tags, one closing tag.+DAMAGED_CAPTURE = """+<html>+<head><title>Example shop</title></head>+<body>+<noscript><iframe src="about:blank" height="0" width="0"></iframe><noscript><iframe src="https://www.googletagmanager.com/ns.html?id=GTM-TEST" height="0" width="0"></iframe></noscript>+<div class="site-content">+ <h1>Unstunned. Hand slaughtered. Halal certified.</h1>+ <p>Our entire assortment is strictly halal, prepared by qualified staff.</p>+</div>+</body>+</html>+"""+++def strategy():+ return LXMLWebScrapingStrategy()+++def test_body_nested_in_unclosed_noscript_is_kept(strategy):+ result = strategy.scrap("https://example.com/", DAMAGED_CAPTURE)++ assert "Unstunned. Hand slaughtered. Halal certified." in result.cleaned_html+ assert "strictly halal" in result.cleaned_html+++def test_balanced_noscript_is_still_removed(strategy):+ html = (+ "<html><body>"+ "<noscript><p>Please enable JavaScript</p></noscript>"+ "<p>Real page copy that should be kept.</p>"+ "</body></html>"+ )++ result = strategy.scrap("https://example.com/", html)++ assert "Please enable JavaScript" not in result.cleaned_html+ assert "Real page copy that should be kept." in result.cleaned_html+++def test_unwrap_leaves_well_formed_documents_untouched():+ html = "<html><body><noscript><p>fallback</p></noscript><p>copy</p></body></html>"++ assert unwrap_orphaned_noscript(html) == html+++def test_unwrap_removes_tags_but_keeps_contents():+ unwrapped = unwrap_orphaned_noscript(DAMAGED_CAPTURE)++ assert "noscript" not in unwrapped+ assert "<h1>Unstunned. Hand slaughtered. Halal certified.</h1>" in unwrapped