How to select nth element of particular type in enlive?

后端 未结 1 2040
一生所求
一生所求 2021-02-10 20:23

I am trying to scrape some data from a page with a table based layout. So, to get some of the data I need to get something like 3rd table inside 2nd table inside 5th table insid

1条回答
  •  孤独总比滥情好
    2021-02-10 21:11

    For nth-of-type, does the following example help?

    user> (require '[net.cgrand.enlive-html :as html])
    user> (def test-html
               "

    first

    second

    third

    ") #'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"]})

    No idea about the second issue. Your approach seems to work with a naive test:

    user> (def test-html "

    in div

    not in div

    ") #'user/test-html user> (html/select (html/html-resource (java.io.StringReader. test-html)) [:body :> :p]) ({:tag :p, :attrs nil, :content ["not in div"]})

    Any chance of looking at your actual HTML?

    Update: (in response to the comment)

    Here's another example where "the second

    inside the

    inside the second
    inside whatever" is returned:

    user> (def test-html "

    this is not the one

    nor this

    or for that matter this

    skip this one too

    definitely not this one

    not this one

    not this one either

    not this one, but almost

    this one

    certainly not this one

    ") #'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 讨论(0)
提交回复
热议问题