Web Scraping
Web scraping is two jobs: fetch a page over HTTP, then pull the parts you want out of its HTML. The example in source-code/web_scraping/ does both. It uses Cohttp with a TLS backend to fetch a page over HTTPS, and lambdasoup to parse the HTML and select elements with CSS selectors.
Parsing HTML
The file src/scraper.ml holds the two extractors, and they are short because lambdasoup does the heavy lifting. The first collects all the visible text on the page:
1 let extract_texts html =
2 Soup.parse html
3 |> Soup.texts
4 |> List.map String.trim
5 |> List.filter (fun s -> s <> "")
6 |> String.concat " "
Soup.parse turns an HTML string into a document. Soup.texts returns every text node. The rest of the pipeline trims each piece, drops the blank ones, and joins the rest with single spaces, so you get the page’s readable text as one clean string.
The second extractor collects every link. Because lambdasoup supports CSS selectors, this is nearly a one-liner:
1 let extract_links html =
2 Soup.parse html
3 |> Soup.select "a[href]"
4 |> Soup.to_list
5 |> List.filter_map (Soup.attribute "href")
Soup.select "a[href]" finds every <a> tag that has an href attribute, the same selector you would use in CSS or the browser console. Soup.to_list turns the selection into a list, and List.filter_map (Soup.attribute "href") reads each tag’s href, dropping any that lack one. The CSS selector replaces the manual tag-walking a lower-level HTML parser would need.
The interface file src/scraper.mli keeps the module’s surface to just these two functions:
1 val extract_texts : string -> string
2 val extract_links : string -> string list
Both take an HTML string, so they are pure and easy to test against a fixture with no network.
Fetching a Page
The program in bin/main.ml fetches a URL and prints its headers, text, and links. Fetching is a short Lwt function:
1 open Lwt.Syntax
2
3 let fetch url =
4 let* resp, body = Cohttp_lwt_unix.Client.get (Uri.of_string url) in
5 let* body_str = Cohttp_lwt.Body.to_string body in
6 Lwt.return (Cohttp.Response.headers resp, body_str)
Cohttp_lwt_unix.Client.get sends the request and returns a response and a streaming body. The let* from Lwt.Syntax waits for each promise, the same syntax the SPARQL client used. Cohttp_lwt.Body.to_string reads the whole body into a string, and the function returns the response headers alongside it.
One dependency makes the HTTPS work. The bin/dune file lists tls-lwt:
1 (executable
2 (name main)
3 (libraries scraper cohttp-lwt-unix lwt.unix uri tls-lwt))
tls-lwt is the TLS backend that Cohttp uses for https:// URLs. Without it, Cohttp can speak plain HTTP but not HTTPS. You do not call it directly; linking it in is enough.
Putting It Together
The main function fetches the page, prints the three sections, and wraps the whole thing in Lwt.catch so a network failure prints a message instead of crashing:
1 let () =
2 let url = "https://markwatson.com/" in
3 Lwt_main.run
4 (Lwt.catch
5 (fun () ->
6 let* headers, body = fetch url in
7 print_endline "Headers:";
8 print_string (Cohttp.Header.to_string headers);
9 print_endline "\nText Content:";
10 print_endline (Scraper.extract_texts body);
11 print_endline "\nLinks:";
12 List.iter print_endline (Scraper.extract_links body);
13 Lwt.return_unit)
14 (fun exn ->
15 Printf.printf "Network error: %s\n" (Printexc.to_string exn);
16 Lwt.return_unit))
Lwt_main.run starts the event loop and runs the whole action. The body fetches the page and prints its headers, its visible text, and every link. The handler turns any exception, a DNS failure or a dropped connection, into a printed line.
Running It
1 cd source-code/web_scraping
2 dune build
3 dune exec ./bin/main.exe
4 dune runtest
The program prints the response headers, the page text as one string, and one link per line. The test runs offline: it parses a local HTML fixture of the same shape and checks that extract_texts and extract_links return the expected values, so dune runtest needs no network. Splitting the pure extractors from the network fetch is what makes that offline test possible, and it is a pattern worth copying: keep parsing separate from I/O, and you can test the hard part without a server.