Handle bot-blocked pages honestly: fetch fallback, degradation flags, thin threshold
- renderer: plain-fetch escalation when the browser render is blocked (4xx) or fails — some sites 403 automated browsers but serve plain HTTP clients - orchestrator: blocked (>=400), empty, and thin results now appear in failed_urls and the degraded trace list instead of passing silently; blocked/thin results are never cached - extractor: thin threshold raised to 500 chars (env: MIN_TEXT_CHARS) so block/challenge interstitials are flagged instead of passing as content Outcome on TechPowerUp (fingerprints Chromium AND Firefox, JS-challenges plain clients): still blocked — accepted per PRD §9 — but results are now flagged thin/degraded so the calling LLM knows to rely on other sources. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
parent
4f3dc39088
commit
4bcbe8daff
3 changed files with 49 additions and 2 deletions
|
|
@ -5,13 +5,16 @@ is "thin" when it's shorter than MIN_TEXT_CHARS; thin/empty primary output
|
|||
triggers the fallback chain, and the longest result wins.
|
||||
"""
|
||||
|
||||
import os
|
||||
import re
|
||||
|
||||
import trafilatura
|
||||
from readability import Document
|
||||
from lxml import html as lxml_html
|
||||
|
||||
MIN_TEXT_CHARS = 200
|
||||
# Real content pages are essentially never this short; block/challenge
|
||||
# interstitials ("Automated bot check in progress...") routinely are.
|
||||
MIN_TEXT_CHARS = int(os.environ.get("MIN_TEXT_CHARS", "500"))
|
||||
|
||||
|
||||
def _trafilatura_extract(html: str, url: str, favor_recall: bool) -> str:
|
||||
|
|
|
|||
|
|
@ -117,7 +117,8 @@ async def scrape_url(client: httpx.AsyncClient, url: str, cache: Cache | None =
|
|||
"extractor_used": extracted.get("extractor_used"),
|
||||
"thin": extracted.get("thin", False),
|
||||
}
|
||||
if cache and result["content"] and not result["thin"]:
|
||||
blocked = result["status"] is not None and result["status"] >= 400
|
||||
if cache and result["content"] and not result["thin"] and not blocked:
|
||||
await cache.set_json(cache_key, result, config.EXTRACT_CACHE_TTL)
|
||||
return {**result, "trace": trace.as_dict()}
|
||||
|
||||
|
|
@ -155,8 +156,15 @@ async def search_contents(
|
|||
"title": page.get("title") or meta["title"],
|
||||
"metadata": page.get("metadata", {}),
|
||||
"final_url": page.get("final_url"),
|
||||
"status": page.get("status"),
|
||||
"thin": page.get("thin", False),
|
||||
}
|
||||
)
|
||||
# Blocked (4xx/5xx) or empty/thin pages are quality failures even
|
||||
# though the render itself "succeeded" — surface them, don't hide them.
|
||||
status = page.get("status")
|
||||
if (status and status >= 400) or page.get("thin") or not page["content"]:
|
||||
failed.append(meta["url"])
|
||||
out.append(entry)
|
||||
if failed:
|
||||
trace.degraded.append("contents")
|
||||
|
|
|
|||
|
|
@ -40,6 +40,30 @@ app.get("/health", async (req, res) => {
|
|||
});
|
||||
});
|
||||
|
||||
// Some sites (e.g. TechPowerUp) fingerprint and 403 automated browsers while
|
||||
// serving plain HTTP clients normally. When the browser render is blocked or
|
||||
// fails, escalate to a plain fetch — server-rendered pages still extract fine.
|
||||
async function plainFetch(url, timeoutMs) {
|
||||
const resp = await fetch(url, {
|
||||
headers: {
|
||||
"User-Agent":
|
||||
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36",
|
||||
Accept: "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
|
||||
"Accept-Language": "en-US,en;q=0.9",
|
||||
},
|
||||
redirect: "follow",
|
||||
signal: AbortSignal.timeout(timeoutMs),
|
||||
});
|
||||
const html = await resp.text();
|
||||
return {
|
||||
html,
|
||||
status: resp.status,
|
||||
final_url: resp.url,
|
||||
rendered_at: new Date().toISOString(),
|
||||
via: "fetch",
|
||||
};
|
||||
}
|
||||
|
||||
app.post("/render", async (req, res) => {
|
||||
const { url, wait_policy, timeout_ms, bypass_cache } = req.body ?? {};
|
||||
if (!url || typeof url !== "string" || !/^https?:\/\//i.test(url)) {
|
||||
|
|
@ -70,6 +94,18 @@ app.post("/render", async (req, res) => {
|
|||
if (attempt < RETRIES) log("warn", "render retry", { url, attempt: attempt + 1 });
|
||||
}
|
||||
}
|
||||
if (result && !result.via) result.via = "browser";
|
||||
if (!result || (result.status && result.status >= 400)) {
|
||||
try {
|
||||
const fetched = await plainFetch(url, timeout_ms ?? TIMEOUT_MS);
|
||||
if (fetched.status < 400 && fetched.html) {
|
||||
log("info", "plain-fetch fallback succeeded", { url, browser_status: result?.status ?? null });
|
||||
result = fetched;
|
||||
}
|
||||
} catch (err) {
|
||||
log("warn", "plain-fetch fallback failed", { url, error: String(err.message) });
|
||||
}
|
||||
}
|
||||
if (!result) throw lastErr;
|
||||
await cache.set(cacheKey, JSON.stringify(result), CACHE_TTL);
|
||||
res.json({ ...result, cache: "miss" });
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue