2010-04-23 56 views
4

我想用基于表格的布局从页面上刮取一些数据。因此,为了获得一些数据,我需要在第一张桌子内的第五张桌子内的第二张桌子内获得第三张桌子之类的东西。我试图使用活泼,但不知道如何使用n型和其他选择器的步骤。更糟糕的是,有问题的页面在主体中有一个顶级表,但是(select data [:body:>:table])由于某种原因返回6个结果。我到底做错了什么?如何选择特定类型的第n个元素有活力?

回答

7

对于nth-of-type,以下示例有帮助吗?

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?

更新:(响应评论)

这里就是 “内的任何第二<div><div>内第二<p>” 返回另一个例子:

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"]}) 
+0

好像第二个问题可能是由于错误的HTML。我可以将n型与其他选择器结合使用吗?如果我需要在第二个表中查找第二个表,我可以做一些类似于[:table(第2类):>:table(第2个类型)]的东西吗? – 2010-04-23 08:44:53

+0

是的,你可以。我已经编辑了一个新的例子。 HTH。 – 2010-04-23 13:26:30

+1

啊! []是十字路口!启蒙已近! – 2010-04-23 20:47:16

相关问题