Skip to content

Instantly share code, notes, and snippets.

@jackrusher
Last active December 19, 2015 11:29
Show Gist options
  • Save jackrusher/5947710 to your computer and use it in GitHub Desktop.
Save jackrusher/5947710 to your computer and use it in GitHub Desktop.
Fetch a page, following any redirects on the way, return the page as an Enlive resource.
;; follows redirects
(defn fetch-page [url]
(enlive/html-resource
(loop [final-url (java.net.URL. url)]
(let [conn (.openConnection final-url)]
(if (re-find #"30[1-3]" (.getHeaderField conn 0))
(recur (java.net.URL. (.getHeaderField conn "location")))
final-url)))))
;; grab page title string
(first (enlive/select (fetch-page "http://google.com") [:title enlive/text]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment