Search code examples
clojurejsoupclojure-java-interop

What is the best way to delete elements from a JSoup Document in Clojure (java mutable object interoperability)?


Complete beginner with Clojure. How do you access the mutated jsoup Document in Clojure? I have the code below where I would like to print out the changed html rather than the links that are being removed.

(defn get-page []
  (.get (org.jsoup.Jsoup/connect "https://example.com")))

(defn -main
  "Fetch the page, delete links, and print out the html of the modified page"
  [& args]
  (let [html (get-page)]
   (println (.remove (.select html "a[href]")))))

Solution

  • @cfrick answered the question in a comment, so I'll just expand that into an example to make it more clear.

    Let's change -main to print the value in html before and after it's changed

    (defn -main
      "Fetch the page, delete links, and print out the html of the modified page"
      [& args]
      (let [html (get-page)]
        (println "html before modification")
        (println html)
    
        (.remove (.select html "a[href]"))
    
        (println "html after modification")
        (println html)))