gpt4 book ai didi

goquery:到达另一个元素时停止解析

转载 作者:IT王子 更新时间:2023-10-29 02:03:39 26 4
gpt4 key购买 nike

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

<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 跳过位于某个标签和类名下面的元素?感谢您提供任何提示!

最佳答案

是的,实际上很简单:

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

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

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

关于goquery:到达另一个元素时停止解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41578768/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com