Skip to content

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.

fix(prefetch): honor <head><base href> in quick_extract_links (#752)

unclecode62c619d454f6
diff --git a/crawl4ai/utils.py b/crawl4ai/utils.py
index 4b3d9690..89fb782d 100644
--- a/crawl4ai/utils.py
+++ b/crawl4ai/utils.py
@@ -2486,7 +2486,23 @@ def quick_extract_links(html: str, base_url: str) -> Dict[str, List[Dict[str, st
except Exception:
return {"internal": [], "external": []}
+ # base_domain stays anchored to the real page origin so internal/external
+ # classification is correct even when a <base> tag points elsewhere.
base_domain = get_base_domain(base_url)
+
+ # Honor <head><base href> for relative-URL resolution (#752). The full LXML
+ # scraping path does this; the prefetch quick path (used by scan/site) must
+ # too — otherwise relative links resolve against the page URL and the base
+ # tag is ignored. urljoin handles a relative <base href> against the page.
+ try:
+ base_el = doc.xpath("//head/base[@href]")
+ if base_el:
+ base_href = (base_el[0].get("href") or "").strip()
+ if base_href:
+ base_url = urljoin(base_url, base_href)
+ except Exception:
+ pass
+
internal: List[Dict[str, str]] = []
external: List[Dict[str, str]] = []
seen: Set[str] = set()