Skip to content

About

Web Scraper in Go, similar to BeautifulSoup

Resources

Stars

0 stars

Watchers

0 watching

Forks

 
 

Repository files navigation

soup (TypeScript)

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

Install

npm install
npm run build

Usage

The 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 idioms, and how they map

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.

Async

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.

API

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

Scripts

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)

Verification

See VERIFICATION.md for the full methodology, the three bugs the differential harness caught, and the documented behavioural differences.

License

MIT, same as the original.

About

Web Scraper in Go, similar to BeautifulSoup

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages