query html document with css selectors
selector | description |
---|---|
: | id [:main] |
. | class [.content.home-page] |
tag | tagname [p.demo a] |
> | direct children [.article > p] |
* | all nodes [:section > *] |
- use
:
for id not#
- leave spaces around
>
, like[:app.items > .item > a]
#+BEGIN_SRC elisp (require 'enlive) (enlive-text (enlive-query (enlive-fetch "http://gnu.org/") [title])) #+END_SRC #+RESULTS: : The GNU Operating System and the Free Software Movement
#+BEGIN_SRC elisp :results raw (enlive-query-all (enlive-parse "<div class=\"content\"> <p>p1</p> <p>p2</p> </div>") [.content p]) #+END_SRC #+RESULTS: ((p nil p1) (p nil p2))
- enlive-query-all will return all node mached
#+BEGIN_SRC elisp :results raw (macroexpand '(enlive-with element (enlive-query [div]) (enlive-query-all [.cls]))) #+END_SRC #+RESULTS: (progn (enlive-query element [div]) (enlive-query-all element [.cls]))
#+BEGIN_SRC elisp :results raw (enlive-let (enlive-parse "<span><i class=\"foo\">foo</i><i class=\"bar\">bar</i></span>") ((foo [.foo]) (bar [.bar])) (list (enlive-text (car foo)) (enlive-text (car bar)))) #+END_SRC #+RESULTS: (foo bar)
enlive-get-element-by-id
enlive-get-elements-by-tag-name
enlive-get-elements-by-class-name
enlive-has-class
enlive-attr