A TypeScript port of github.com/anaskhan96/soup — a simple web scraper, kept as similar as possible to BeautifulSoup.
- Source:
github.com/anaskhan96/soup@260263cac6a14c508448b87549ecd7928bf9d75d - Parity: 20,657 behavioural observations verified byte-identical against the Go original
- Tests: all 21 upstream test cases ported 1:1, plus 90 supplementary ones
npm install
npm run buildThe API mirrors the Go package. Method names, argument order and return shapes are unchanged.
import * as soup from 'soup';
const [html, err] = await soup.Get('https://xkcd.com/353');
if (err !== null) throw err;
const doc = soup.HTMLParse(html);
doc.Find('div', 'id', 'ctitle').Text(); // "Python"
doc.Find('div', 'id', 'comic').Find('img').Attrs()?.['src'];
doc.FindAll('a').map((a) => a.Attrs()?.['href']);
doc.Find('ul').Find('li').FullText();
doc.Find('ul').Find('li').HTML();| Go | TypeScript |
|---|---|
body, err := soup.Get(url) |
const [body, err] = await soup.Get(url) |
soup.Error |
soup.SoupError (also exported as soup.Error) |
err.(soup.Error).Type |
(err as SoupError).Type |
err.Error() |
err.Error() — or err.message |
r.Attrs()["src"] (nil map ⇒ "") |
r.Attrs()?.['src'] ?? '' |
url.Values{"k": {"v"}} |
new soup.Values({ k: ['v'] }) |
url.ParseQuery(s) |
const [v, err] = soup.parseQuery(s) |
Functions returning (T, error) in Go return a [T, Error | null] tuple here,
so the error-handling shape of call sites is preserved rather than being
converted to exceptions.
Get, GetWithClient, Post, PostWithClient and PostForm are async and
return promises. Everything else — parsing, traversal, Text, FullText,
HTML, Attrs — is synchronous, exactly as in Go.
Same surface as the Go package:
Parsing / traversal — HTMLParse, Root.Find, FindAll, FindStrict,
FindAllStrict, FindNextSibling, FindPrevSibling, FindNextElementSibling,
FindPrevElementSibling, Children, Attrs, Text, FullText, HTML
HTTP — Get, GetWithClient, Post, PostWithClient, PostForm,
Header, Cookie, Headers, Cookies, SetDebug
Errors — SoupError/Error, ErrorType, newError, and the eleven
Err* constants
npm test # 111 tests, incl. byte-for-byte parity against Go goldens
npm run coverage # coverage report (90.1% statements)
npm run typecheck # tsc --noEmit
npm run build # emit dist/
npm run verify # live differential run against the real Go package (needs Go)See VERIFICATION.md for the full methodology, the three bugs the differential harness caught, and the documented behavioural differences.
MIT, same as the original.