Comments (7)
Github really need to provide some kind of preview feature...
#{[:some :selector] [:another :selector]}
from enlive.
this is the grouping selector though right? I want to shortcut if the first selector matches, does this make sense?
from enlive.
Yep, after having slept on it, it occurred to me that what you are asking for is a short-circuiting or. Real short-circuiting is nearly impossible because selection is normally performed on a single pass over the whole tree, and to short-circuit you need to know if the first part of the selector matches something.
I said "normally" because (has xxx) introduce a second selection and, hence, a second pass.
The simplest thing to do is to write a macro on select which outputs:
(let [YYY XXX](or %28seq %28select YYY [:some :selector]%29%29 %28select YYY [:another :selector]%29))
I'm thinking on it.
from enlive.
(defn select-else* [nodes & states] ; for completeness only
(some #(seq (select* nodes %)) states))
(defmacro select-else [nodes & selectors]
(let [nodes-sym (gensym "nodes")]
`(let [~nodes-sym ~nodes]
(or
~@(map (fn [sel]
`(seq (select ~nodes-sym ~sel)))
selectors)))))
I tried to think of a (or-else) combinator (to write (or-else [:some :selector] [:another :selector])), it's doable but it really goes against the grain.
Does select-else (or select-else*) do the trick?
from enlive.
(select xml #{[:ASIN], [:title], [:Item :> :MediumImage :URL], [:ListPrice :Amount] [:ListPrice :FormattedPrice] [:Publisher]}
Not really. Here's a real example. [:ListPrice :FormattedPrice] doesn't always match. Sometimes the formatted price appears elsewhere. I want to be able to provide an ordered list of possible selectors and short circuit soon as we find one.
If this isn't possible I can come up with a work around.
For XML data sources that are smart enough not to bother with namespacing all their elements, Enlive is a EXCELLENT tool for extracting data.
(def *aws-base-url* "http://ecs.amazonaws.com/onca/xml") (def *aws-url-params* {:Service "AWSECommerceService", :AWSAccessKeyId "AKIAI7BLMMASTXEDYLWA", :Operation "ItemLookup", :ResponseGroup "Medium"}) (defn aws-url [itemid] (str *aws-base-url* "?" (to-url-params (assoc *aws-url-params* :ItemId itemid)))) (defn amazon-data [itemid] (html-resource (java.net.URL. (aws-url itemid)))) (defn item-data [xml] (let [item-data (select xml #{[:ASIN], [:title], [:Item :> :MediumImage :URL], [:ListPrice :Amount] [:ListPrice :FormattedPrice] [:Publisher]})] (apply hash-map (flatten (map #(vector (:tag %) (:content %)) item-data))))) (defn amazon-info [itemid] (item-data (amazon-data itemid)))
Is all I need to extract info from Amazon on any item. Enlive is a killer app ;)
from enlive.
namespace support is on my todo-list.
completely untested (I'll have more time in the afternoon (CEST)):
(defmacro or-else
([sel](selector ~sel))
([sel & sels]
`(sm/union (selector ~sel)
(sm/intersection
(sm/complement-next (has* (selector ~sel1)))
(or-else ~@Sels)))))
(btw in which TZ are you? I thought you were on the east coast)
from enlive.
Closing the issue. Performing multiple passes is the way to go.
from enlive.
Related Issues (20)
- Automated CSS selector translation to vector form HOT 5
- [org.jsoup/jsoup "1.8.3"] exists HOT 1
- Make JSoup the default for enlive 1.2 HOT 1
- StackOverflowError when parsing certain html HOT 4
- Composable selector/transform in snippet HOT 1
- Simple variable substitution (with example) in meta tag throwing NullPointerException HOT 2
- Less obscure error messages
- first-child
- End tag for <col> violates the specs
- javax.net.ssl.SSLHandshakeException while trying to reach some resources HOT 1
- attribute matching inside at-form HOT 1
- "safe write" "feature" in IntelliJ prevents auto-reloading of namespace depending on changed HTML
- Removing element cleanly HOT 2
- Is this project dead? HOT 1
- Update jsoup dependency to v1.11.3+
- Setting HTTP headers HOT 1
- Broken wiki link in project description
- HTML resource not found
- Any interest in CSS-style selector syntax option? HOT 1
- [BUG] Some tags are loosing part of their content when selected.
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from enlive.