首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何编写以下clojure enlive选择器?

如何编写以下clojure enlive选择器?
EN

Stack Overflow用户
提问于 2016-01-08 23:15:06
回答 2查看 650关注 0票数 1

我正在尝试使用clojure的enlive库来抓取一个网站。对应的CSS选择器是:

代码语言:javascript
复制
body > table:nth-child(2) > tbody > tr > td:nth-child(3) > table > tbody > tr > td > table > tbody > tr:nth-child(n+3)

我已经使用jquery测试了上面的选择器,它可以工作。但我不知道如何翻译上面的enlive的选择器语法。我试着写一些大意如下的东西:

代码语言:javascript
复制
(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的语法。

非常感谢。

编辑:以包含完整的代码。

EN

回答 2

Stack Overflow用户

发布于 2016-01-08 23:24:40

您缺少的语法是使用伪选择器的元素周围附加的一组括号。所以你想要这样的东西

代码语言:javascript
复制
 [: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)]])
票数 0
EN

Stack Overflow用户

发布于 2017-01-09 23:03:12

它看起来像是浏览器(至少我的firefox版本)在它们的DOM表示中添加了tbody标记,即使它不是在实际的源代码中。

Enlive不这么做。因此,当您省略tbody部分时,您的代码应该可以工作。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34687654

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档