Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add usage examples #14

Open
ustun opened this issue Oct 16, 2014 · 2 comments
Open

Add usage examples #14

ustun opened this issue Oct 16, 2014 · 2 comments

Comments

@ustun
Copy link

ustun commented Oct 16, 2014

Once the html is parsed, how can most efficiently query the parsed document? That is, I would want to be able to drill down as if it were a map:

(get-in x [:html :head :title])

It would be great if you added some recommendations how to do that transformation (for example https://github.com/cjohansen/hiccup-find looks promising).

@collinalexbell
Copy link

Ditto this. As a clojure noob, this vector thing confuses the hell out of me

@nathell
Copy link
Owner

nathell commented Mar 4, 2015

Thanks for chiming in!

A quick-and-dirty solution could be something along the lines of (untested, might be buggy):

(defn get-in-html [tree [tag & tags]]
  (if tag
    (when tree
      (recur (first (filter #(= (first %) tag) (rest tree))) tags))
    tree))

Note that you'd want to call it as (get-in x [:head :title]), bypassing the :html.

This is very simplistic and only supports seqs of tags. If you want to extract arbitrary subtrees, you may want to take a look at Enlive. (When I have free time, I intend to explore the possibility of integrating clj-tagsoup and Enlive, as I feel that both projects might benefit from this.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants