Web scraping with Plump
File: webscraping.lisp
When a page is plain HTML, you do not need a browser: fetch it with curl, parse it into a DOM with Plump, and walk the tree.
The code
1 ;; sbcl --script webscraping.lisp (needs Quicklisp + curl)
2 #-quicklisp (load "~/quicklisp/setup.lisp")
3 (ql:quickload '(:plump :clss :cl-ppcre) :silent t)
4
5 (defun fetch (url)
6 (plump:parse
7 (uiop:run-program
8 (list "curl" "-fsL" "-A" "Mozilla/5.0" url) :output :string)))
9
10 (defun text (node)
11 "Text of NODE, skipping boilerplate; block tags become newlines."
12 (let ((tag (and (plump:element-p node)
13 (string-downcase (plump:tag-name node)))))
14 (cond ((plump:text-node-p node)
15 (cl-ppcre:regex-replace-all "\\s+" (plump:text node) " "))
16 ((member tag
17 '("script" "style" "head" "nav" "header"
18 "footer" "noscript" "iframe")
19 :test #'equal)
20 "")
21 ((plump:nesting-node-p node)
22 (let ((s (format nil "~{~a~}"
23 (map 'list #'text (plump:children node)))))
24 (if (member tag '("p" "div" "li" "br" "tr" "h1" "h2"
25 "h3" "h4" "h5" "h6" "section"
26 "article" "ul" "ol" "pre" "blockquote")
27 :test #'equal)
28 (format nil "~%~a~%" s)
29 s)))
30 (t ""))))
31
32 (defun clean (s)
33 (string-trim
34 '(#\Space #\Newline)
35 (cl-ppcre:regex-replace-all
36 "\\n{3,}"
37 (cl-ppcre:regex-replace-all "(?m)^ +| +$" s "")
38 (format nil "~%~%"))))
39
40 (defun dom (page)
41 "PAGE is either a URL string or an already-parsed plump DOM."
42 (if (stringp page) (fetch page) page))
43
44 (defun headers (page)
45 (map 'list (lambda (h) (clean (text h)))
46 (clss:select "h1,h2,h3,h4,h5,h6" (dom page))))
47
48 (defun page-text (page) (clean (text (dom page))))
49
50 (let ((root (fetch "https://markwatson.com")))
51 (print (headers root))
52 (format t "~%~%~a~%" (page-text root)))
How it works
- Three libraries do most of the work:
plumpparses HTML into a DOM,clssadds CSS-selector queries over that DOM, andcl-ppcreprovides regular expressions for whitespace cleanup. fetchrunscurl -fsLwith a browser-likeUser-Agentand parses the reply.-Lfollows redirects, and-fmakes curl exit with an error on an HTTP 404 or 500 instead of returning the server’s error page as content;uiop:run-programthen signals asubprocess-error.textdoes the main work: a recursive DOM walk that skips boilerplate elements (script,style,nav,header,footer, …), collapses whitespace inside text nodes, and turns block-level tags into newlines so the output keeps the page’s paragraph structure.cleantidies the result: it strips spaces at the start and end of each line, collapses runs of three or more newlines into one blank line, and trims the ends.headersusesclss: one CSS selector finds allh1throughh6elements.domletsheadersandpage-textaccept either a URL or a parsed DOM. The demo uses this to download the page once and run both functions on the same tree.
Running it
1 $ sbcl --script webscraping.lisp
Actual output (showing the full headers list and the opening paragraphs of page-text):
1 ("Mark Watson" "Systems Thinking & Generalist AI"
2 "Read My Books — Many for Free" "Connect & Interests"
3 "Free Mentoring for Technical Projects" "Holistic Systems Architecture"
4 "Neuro-Symbolic Integration" "Multi-Paradigm Polyglot"
5 "40+ Years of Long-Horizon Vision" "Loving Lisp Is Being Rewritten"
6 "Writings & Blogs" "Open Source" "Consulting" "Interests & Music"
7 "How It Works" "Additional eBooks on Leanpub"
8 "Traditional Publications (Springer-Verlag, McGraw-Hill, Morgan Kaufmann)"
9 "Free Semantic Web PDFs & Material")
10
11 Mark Watson
12
13 Systems Thinker & Generalist AI Expert
14
15 Synthesizing complex technologies across 40+ years of computing. I
16 approach artificial intelligence through a systems-thinking lens by
17 bridging modern LLMs, neural networks, knowledge graphs, and symbolic
18 reasoning with large-scale distributed architectures.
19
20 Self-funded open source researcher and author, with engineering and
21 advisory experience for organizations including Google, Mind AI,
22 Capital One, Disney, SAIC/Leidos, DARPA, Babylist, Olive AI, and
23 CompassLabs.
24
25 40+ Years in AI
26
27 20+ Books Published
28
29 55 US Patents
30 ...
Using it
page-text pairs well with the summarizer chapter: summarize any web page with (summarize (page-text url) 3). headers alone is handy for skimming a page’s structure. Compared with the Lightpanda chapter, this approach is faster and needs no external binary, but it never runs JavaScript.