Spec-conformant HTML pages with properly-encoded multi-parameter URLs hand the agent a broken URL
Bug
_extract_links in web/fetcher.py reads href attribute values directly from raw HTML without decoding HTML entities. Because valid HTML requires & inside attribute values to be written as &, a page containing something as:
<a href="https://example.com/search?q=foo&lang=en">Search</a>
causes the fetcher to surface https://example.com/search?q=foo&lang=en as a link. When the web agent later calls fetch_url with that string, the request is sent with a literal & in the query string, which many servers either reject or silently misparse, so the agent ends up fetching the wrong page or getting a 400.
Repro Any real-world page whose query-string links are correctly HTML-encoded (which is the spec-required form) triggers this. For example, Google Search results, GitHub search pages, and most CMS-generated pages encode & as & in href attributes.
Confirming this from another implementation, and it points at a clean fix: the
root cause is regex-scraping href out of raw HTML, which leaves the entity
encoding intact. A real HTML parser decodes attribute values for you, so the bug
just disappears.
For comparison, okf-kit extracts links with selectolax (a DOM parser) and the same input comes out already decoded:
<a href="...?q=foo&lang=en&page=2">
→ https://example.com/search?q=foo&lang=en&page=2 (no literal &)
So two options:
- Minimal — HTML-unescape the match before using it:
html.unescape(href)decodes&,&, etc. Cheap, keeps the existing regex. - Sturdier — pull
hreffrom an HTML parser (selectolax / lxml / BeautifulSoup) instead of regex. Decoding is automatic, and you also stop tripping on adjacent edge cases:hrefsplit across lines, single vs. double quotes, or&inside other attributes.
Worth flagging the mirror image, since it bites on output too: a URL you scraped
should be stored decoded (&), and any HTML you later emit re-encodes it.
Round-tripping through raw strings is where these regressions creep back in — a
parse-in / serialize-out discipline avoids both directions.
Confirmed from another implementation (I maintain okf-kit, an independent OKF producer/consumer) — the root cause is regex-scraping href out of raw HTML, which leaves the entity encoding intact. Opened #174 with a fix.
It parses href with BeautifulSoup (already a dependency via markdownify) instead of the regex, so attribute entities decode per the HTML spec. One subtlety worth flagging: the tempting one-liner html.unescape(href) is itself wrong for URLs — it over-decodes query params that match no-semicolon legacy entities, turning ?a=1©=2®=3 into ?a=1©=2®=3. Parsing the attribute avoids that (&copy= → ©=, not ©), and there's a regression test for it.