3

I am using enlive clojure to parse HTML. My parser looks like;

(def each-rows
  (for [tr crawl-page
        :let [row (html/select tr [:td (attr= :class "bl_12")])]
        :when (seq row)]
    row))

which extracts result as following;

  {:tag :a,
   :attrs
   {:class "bl_12",
    :href
    "url1"},
   :content ("Chapter 1")}
  {:tag :a,
   :attrs
   {:class "bl_12",
    :href
    "url2"},
   :content ("Chapter 2")}
  {:tag :a,
   :attrs
   {:class "bl_12",
    :href
    "url3"},
   :content ("Chapter 3")}

Now my objective is to get a dictionary like this;

   {:Chapter_1 "url1"  
   :Chapter_2 "url2"
   :Chapter_3 "url3"}

I managed to write a method which extracts only href or only content, but couldn't make it as a map

 (defn read-specific-other [x]
  (map (comp second :attrs) x))

output : [:href "url1"]

  (defn read-specific-content [x]
    (map (comp first ::content) x))

(map read-specific-content each-rows)

output :

(("Chapter 1"
"Chapter 2"
"Chapter 3"
))

How do I get the desired result

2
  • Hi, I am considering using Clojure to parse XML. Did you choose Clojure because (a) it is more efficient or (b) it is the language you just use? Commented Dec 28, 2015 at 3:23
  • 1
    I am working on crawling and data processing & clojure is very powerful and natural to do such things, pipeline is super efficient (Thread macro) , I choosed clojure for its natural power of data processing Commented Dec 28, 2015 at 11:04

1 Answer 1

2

Take a look at zipmap

(zipmap (read-specific-other each-rows) (read-specific-content each-rows))

If you really want the keys to be keywords, then use the keyword function; but I recommend keeping strings as the keys.

Also consider using an into for pattern instead:

(into {}
  (for [[{:keys [attrs]} {:keys [content]}] rows]
    [content attrs]))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.