2010-04-23 52 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

相關問題