Skip to content

Instantly share code, notes, and snippets.

Last active Feb 13, 2016
What would you like to do?
(defn login []
(let [
;;;; Use clj-http's cookie-store.
cookie-store (cookies/cookie-store)
;;;; Get the CSRF token and cookies from the login page.
login-page (client/get
{:cookie-store cookie-store})
body (html/html-resource ( (:body login-page)))
token-div (html/select body [(html/attr= :name "authenticity_token")])
token (get-in (into {} token-div) [:attrs :value])
;;;; Use that CSRF token, cookies, and credentials to actually log in.
;;;; The actual url to log in is slightly different, for whatever reason.
logmein (client/post ""
{:form-params {:email_address ""
:password "12345"
:authenticity_token token}
:cookie-store cookie-store})]
;;;; (:status logmein) will show 302, so we have logged in.
(defn fetch-url [url]
(-> url clj-http.client/get :body html/html-resource))
;;;; Below:
;;;; First log in, then go get the secured page.
;;;; Why? Because clj-http's docs *seem* to say future GET requests will use the
;;;; acquired cookies automatically.
;;;; See
(defn profile-scraper []
(let [login (login)
dom (fetch-url "http://www.generic-website/secured-page")
address (html/select dom [(html/attr= :itemprop "address") html/text-node])]
;;;; Sadly, this function returns only non-secured information.
;;;; I have tried including {:cookie-store cookie-store} with the GET request as well.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment