【问题标题】:goquery: stop parsing when another element is reachedgoquery:到达另一个元素时停止解析
【发布时间】:2017-01-10 21:29:41
【问题描述】:

假设我有这个 HTML 页面。我想用Gogoquery解析它:

<html>
    <head><!--Page header stuff--></head>
    <body>
         <h1 class="h1-class">Heading 1</h1>
             <div class="div-class">Stuff1</div>
             <div class="div-class">Stuff2</div>
         <h1 class="h1-class">Heading 2</h1>
             <div class="div-class">Stuff3</div>
             <div class="div-class">Stuff4</div>
    </body>
</html>

碰巧,我只想在标题 2 之前获取那些 DIV,然后跳过其余部分。此代码非常适合获取 所有 DIV:

 doc := GetGoQueryDocument(url) //Defined elsewhere
 doc.Find("div.div-class").Each(func(_ int, theDiv *goquery.Selection){
     //do stuff with each theDiv
     //The problem is that it finds div.div-class elements below Heading 2.
     //I want to skip those.
 })

有没有办法告诉 goquery 跳过位于某个标签和类名下的元素?感谢您的任何提示!

【问题讨论】:

    标签: go goquery


    【解决方案1】:

    是的,其实很简单:

    doc.Find(".h1-class").First().NextUntil(".h1-class")
    

    我建议你通读 godoc:https://godoc.org/github.com/PuerkitoBio/goquery

    它解释了您可以操纵选择的所有不同方式。

    【讨论】:

      猜你喜欢
      • 2022-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-30
      相关资源
      最近更新 更多