Skip to content

Commit aa0c465

Browse files
committed
added enlive version
1 parent 3aa8506 commit aa0c465

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
(ns clojure-scraping-overview.reddit-meta-enlive
2+
(:require [clj-http.client :as client]
3+
[clojure-scraping-overview.core :as core]
4+
[net.cgrand.enlive-html :as html]))
5+
6+
(defn get-link
7+
[a-link]
8+
(-> a-link client/get :body))
9+
10+
(def *title-selector* [:div#siteTable :p.title])
11+
(def *points-selector* [:div#siteTable :div.midcol :div.score])
12+
(def *username-selector* [:div#siteTable :div.entry :p.tagline :a.author])
13+
(def *comments-selector* [:div#siteTable :div.entry :ul.flat-list :li.first :a.comments])
14+
15+
(defn select-item
16+
[doc sel]
17+
(map
18+
html/text
19+
(html/select
20+
(html/html-resource
21+
(java.io.StringReader. doc))
22+
sel)))
23+
24+
(defn extract-info
25+
[]
26+
(let [document (get-link "http://reddit.com")
27+
28+
titles (select-item document *title-selector*)
29+
points (select-item document *points-selector*)
30+
uname (select-item document *username-selector*)
31+
comments (select-item document *comments-selector*)]
32+
(map
33+
(fn [[t p u c]]
34+
{:title t
35+
:submitter u
36+
:comments c
37+
:points p})
38+
(map vector titles points uname comments))))
39+
40+
(defn extract-info2
41+
[]
42+
(let [document (get-link "http://reddit.com")]
43+
(map
44+
(fn [[t p u c]]
45+
{:title t
46+
:submitter u
47+
:comments c
48+
:points p})
49+
(partition
50+
4
51+
4
52+
(select-item
53+
document
54+
#{*title-selector*
55+
*points-selector*
56+
*username-selector*
57+
*comments-selector*})))))

0 commit comments

Comments
 (0)