LoginSignup
2
2

More than 5 years have passed since last update.

ClojureでJsoupをラップしてみた

Last updated at Posted at 2014-03-06

スクレイピング

Clojureでスクレイピングといえば既にEnliveがあるのだけれど
jQueryライクな表記はClojureのシンボルでは限界あるので
ひとまずJavaのライブラリであるところのJsoupをラップしてみた

emanon-was/cljsoup

使ってみる

1 . 準備


user> (use 'cljsoup.core)
nil

2 . URLから取ってきたHTMLをparseする


user> (def x (parse (url "https://github.com")))
#'user/x

user> x
#<Document <!DOCTYPE html> ... >

3 . Javaのクラスのままでは扱いにくいのでマッピングする


user> (nodes x)
({:type :dtd, :data ["html" "" ""]} "\n" {:tag :html, ...})

4 . CSSセレクタを使う


user> (select x "input[type=text][name=q]")
(#<Element <input type="text" data-hotkey="/ s" name="q" ...>)

user> (nodes (select x "input[type=text][name=q]"))
({:tag :input, :attrs {:type "text", :data-hotkey "/ s", :name "q", ...})

5 . 手続き的に属性(今回はname)を取ってくる


user> (-> x (select "input[type=text]") (attr "name"))
("q" "user[login]" "user[email]")

6 . HTMLを編集する(要素の直後に<div>test</div>を挿入)


user> (transform x "input[type=text]" (append (html [:div "test"])))
#<Document <!DOCTYPE html> ... >

7 . Javaのクラスから文字列に戻す

user> (parse x)
"<!DOCTYPE html> \n<html> \n <head ... "

まとめ

前回書いたdefstrictマクロを盛りまくってディスパッチしまくり
徐々にこれを拡張していってテンプレートエンジンまで持っていきたい

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2