1

clojure の enlive ライブラリを使用して Web サイトをスクレイピングしようとしています。対応する CSS セレクターは次のとおりです。

body > table:nth-child(2) > tbody > tr > td:nth-child(3) > table > tbody > tr > td > table > tbody > tr:nth-child(n+3)

jqueryを使用して上記のセレクターをテストしましたが、動作します。しかし、上記を enlive のセレクター構文に変換する方法がわかりません。私は次の行に沿って何かを書き込もうとしました:

(ns vimindex.core
  (:gen-class)
  (:require [net.cgrand.enlive-html :as html]))

(def ^:dynamic *vim-org-url* "http://www.vim.org/scripts/script_search_results.php?order_by=creation_date&direction=descending")
(defn fetch-url [url]
  (html/html-resource (java.net.URL. url)))

(defn scrape-vimorg []
  (println "Scraping vimorg")
  (println
    (html/select (fetch-url *vim-org-url*)
                 [:body :> [:table (html/nth-child 2)] :> :tbody :> :tr :> [:td (html/nth-child 3)] :> :table :> :tbody :> :tr :> :td :> :table :> :tbody :> [:tr (html/nth-child 1 3)]])))
;                  body  >   table:nth-child(2)         >  tbody  >  tr  >   td:nth-child(3)         >  table  >  tbody  >  tr  >  td  >  table  >  tbody  >   tr:nth-child(n + 3)
; Above selector works with jquery

(defn -main
  [& args]
  (scrape-vimorg))

しかし、空の応答が返されます。上記の CSS セレクターを enlive の構文で翻訳する方法を教えてください。

どうもありがとう。

編集済み:完全なコードを含める。

4

2 に答える 2

0

欠落している構文は、疑似セレクターを使用する要素を囲む括弧の追加セットです。したがって、次のようなものが必要です。

 [:body :> [:table (html/nth-child 2)] :> :tbody :> :tr 
 [:td (html/nth-child 3)] :> :table :> :tbody :> :tr :> :td :> 
 :table :tbody :> [:tr (html/nth-child 1 3)]])
于 2016-01-08T23:24:40.520 に答える
0

ブラウザー (少なくとも私のバージョンの Firefox) は、実際のソースになくても、DOM 表現に tbody タグを追加するようです。

Enlive はそうしません。したがって、tbody 部分を省略してもコードは機能するはずです。

于 2017-01-09T23:03:12.177 に答える