2

I am trying to get data with goquery from an HTML document. The HTML is structured like this:

<div class="container">
  <div class="table-holder body">
    <div class="simple-wrapper">
    </div>
    <div class="simple-mask">
      <table>
        <tbody>
        </tbody>
      </table>
    </div>
  </div>
  <div class="table-holder body">
    <div class="simple-wrapper">
    </div>
    <div class="simple-mask">
      <table>
        <tbody>
        </tbody>
      </table>
    </div>
  </div>
</div>

What I would like to get is the content of tbody of the first Node object. I am getting the first Node like this:

container := doc.Find(".container")
tableHolder := container.Find(".table-holder.body").Nodes[0]

But, since there is no Find method on the Node struct, I am wondering how can I then get the tbody from the Node struct?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Leff
  • 1,968
  • 24
  • 97
  • 201
  • There's a `Find` on the `Selection` returned by `Find`. Did you try storing the result of `Find` (rather then just grabbing its first Node) and then using that to "dig deeper"? – mkopriva Apr 24 '20 at 07:02
  • Not sure how I can use Find in this case when both Nodes have same classes, I need to dig deeper specifically in the first Node – Leff Apr 24 '20 at 07:10
  • Admittedly I've never used goquery and it's been ages since I've use jquery so I may very well be wrong, but I assumed that the selection will hold a set of *all* nodes that matched the query, and subsequent calls to Find on a selection would query the whole set of that selection, returning child nodes of all the nodes from the receiver selection, while preserving the order in which they exist in the html... If this is the case you should be able to then grab the `tbody` just as you've grabbed the `table-holder`... – mkopriva Apr 24 '20 at 07:17
  • ... i.e. `container.Find(".table-holder.body").Find("tbody").Nodes[0]` should work, no? – mkopriva Apr 24 '20 at 07:17
  • That said, if you want to be more explicit you probably could use [`First`](https://godoc.org/github.com/PuerkitoBio/goquery#Selection.First)? i.e. `container.Find(".table-holder.body").First().Find("tbody").Nodes[0]`? – mkopriva Apr 24 '20 at 07:19
  • 1
    I actually had to this to get the rows: ```doc.Find(".container").Find(".table-holder.body").First().Find("tbody").Find("tr")``` – Leff Apr 24 '20 at 07:31
  • https://play.golang.com/p/c7VFxxBN6ue – mkopriva Apr 24 '20 at 08:27

0 Answers0