forked from swannodette/enlive-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscrape1.clj
22 lines (17 loc) · 834 Bytes
/
scrape1.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
(ns tutorial.scrape1
(:require [net.cgrand.enlive-html :as html]))
(def ^:dynamic *base-url* "https://news.ycombinator.com/")
(defn fetch-url [url]
(with-open [inputstream (-> (java.net.URL. url)
.openConnection
(doto (.setRequestProperty "User-Agent"
"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1;)"))
.getContent)]
(html/html-resource inputstream)))
(defn hn-headlines []
(map html/text (html/select (fetch-url *base-url*) [:td.title :a])))
(defn hn-points []
(map html/text (html/select (fetch-url *base-url*) [:td.subtext html/first-child])))
(defn print-headlines-and-points []
(doseq [line (map #(str %1 " (" %2 ")") (hn-headlines) (hn-points))]
(println line)))