Clojure 如何在enlive中选择特定类型的第n个元素?

Clojure 如何在enlive中选择特定类型的第n个元素?,clojure,Clojure,我正试图从基于表格的布局的页面中获取一些数据。所以,为了得到一些数据,我需要得到一些东西,比如第三张桌子里面第二张桌子里面第五张桌子里面第一张桌子里面身体里面。我正在尝试使用enlive,但不知道如何使用第n个类型和其他选择器步骤。更糟糕的是,所讨论的页面在正文中只有一个顶级表,但出于某种原因,select data[:body:>:table]返回6个结果。我到底做错了什么?对于第n种类型,下面的示例有帮助吗 user> (require '[net.cgrand.enlive-html

我正试图从基于表格的布局的页面中获取一些数据。所以,为了得到一些数据,我需要得到一些东西,比如第三张桌子里面第二张桌子里面第五张桌子里面第一张桌子里面身体里面。我正在尝试使用enlive,但不知道如何使用第n个类型和其他选择器步骤。更糟糕的是,所讨论的页面在正文中只有一个顶级表,但出于某种原因,select data[:body:>:table]返回6个结果。我到底做错了什么?

对于第n种类型,下面的示例有帮助吗

user> (require '[net.cgrand.enlive-html :as html])
user> (def test-html
           "<html><head></head><body><p>first</p><p>second</p><p>third</p></body></html>")
#'user/test-html
user> (html/select (html/html-resource (java.io.StringReader. test-html))
                   [[:p (html/nth-of-type 2)]])
({:tag :p, :attrs nil, :content ["second"]})
不知道第二个问题。您的方法似乎适用于一个简单的测试:

user> (def test-html "<html><head></head><body><div><p>in div</p></div><p>not in div</p></body></html>")
#'user/test-html
user> (html/select (html/html-resource (java.io.StringReader. test-html)) [:body :> :p])
({:tag :p, :attrs nil, :content ["not in div"]})
有没有机会看看你的实际HTML

更新:回应评论

下面是另一个示例,其中第二个在第二个内部,返回的内容在第二个内部:

user> (def test-html "<html><head></head><body><div><p>this is not the one</p><p>nor this</p><div><p>or for that matter this</p><p>skip this one too</p></div></div><span><p>definitely not this one</p></span><div><p>not this one</p><p>not this one either</p><div><p>not this one, but almost</p><p>this one</p></div></div><p>certainly not this one</p></body></html>")
#'user/test-html
user> (html/select (html/html-resource (java.io.StringReader. test-html))
                   [[:div (html/nth-of-type 2)] :> :div :> [:p (html/nth-of-type 2)]])
({:tag :p, :attrs nil, :content ["this one"]})

看起来第二个问题可能是由于糟糕的HTML。我可以将第n个类型与其他选择器组合吗?如果我需要在第二个表中找到第二个表,我可以做类似于[:2类的表n:>:2类的表n]的事情吗?啊![]是十字路口!开悟就在眼前!