Search code examples
gogoquery

Scraper Find element by text


for example, I have next table

<table>
  <tr>
    <td>
      First
    </td>
    <td>
      1
    </td>
  </tr>
  <tr>
    <td>
      Second
    </td>
    <td>
      2
    </td>
  </tr>
</table>

how can I find element by text, for example "Second" and then get value "2"?

Of course, I can do something like

doc, _ := goquery.NewDocumentFromReader(resp.Body)
caseSize := doc.Find("tr").Each(func(i int, element *goquery.Selection){
  // here I check each element by needed text
})

but maybe there is may be another, more simple way, some specific finder?


Solution

  • SOLVED

    As goQuery uses jQuery finders, I changed goQuery finder in a next way

    doc.Find("tr:contains('Second')").Find("td").First().Next().Text()