Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse HTML and build a map from the parsed value using clojure

Tags:

html

clojure

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

like image 457
Abhishek Choudhary Avatar asked Nov 26 '25 13:11

Abhishek Choudhary


1 Answers

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]))
like image 141
Timothy Pratley Avatar answered Nov 29 '25 01:11

Timothy Pratley



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!