我正在尝试提取目标属性并从HTML代码段添加到切片
<div class="pagination pagination-responsive">
<ul class="list-unstyled">
<li class="active">
<a rel="start" target="1" href="/s/Cambridge--MA--United-States">1</a>
</li>
<li>
<a rel="next" target="2" href="/s/Cambridge--MA--United-States?page=2">2</a>
</li>
<li>
<a target="3" href="/s/Cambridge--MA--United-States?page=3">3</a>
</li>
<li class="gap"><span class="gap">…</span>
</li>
<li>
<a target="17" href="/s/Cambridge--MA--United-States?page=17">17</a>
</li>
<li class="next next_page"><a target="2" rel="next" href="/s/Cambridge--MA--United-States?page=2">
<span class="screen-reader-only">Next</span><i class="icon icon-caret-right"></i></a>
</li>
</ul>
</div>
</div>
pageCounts := doc.Find(".pagination-responsive .list-unstyled")
for page := range pageCounts.Nodes {
pageIterator := pageCounts.Eq(page)
li := pageIterator.Find("li a")
href, _ := li.Attr("target")
fmt.Println(href)
}
有人可以说明我在这里可能缺少什么吗?
答案 0 :(得分:2)
li := pageIterator.Find("li a")
实际上是一系列元素,但您只需要获取第一个元素的attr。在这方面有点像jquery。你真正想做的是遍历所有链接,Each
将成为你的朋友。我发现它比使用Eq。
这段代码对我有用:
var html = `
<div class="pagination pagination-responsive">
<ul class="list-unstyled">
<li class="active">
<a rel="start" target="1" href="/s/Cambridge--MA--United-States">1</a>
</li>
<li>
<a rel="next" target="2" href="/s/Cambridge--MA--United-States?page=2">2</a>
</li>
<li>
<a target="3" href="/s/Cambridge--MA--United-States?page=3">3</a>
</li>
<li class="gap"><span class="gap">…</span>
</li>
<li>
<a target="17" href="/s/Cambridge--MA--United-States?page=17">17</a>
</li>
<li class="next next_page"><a target="2" rel="next" href="/s/Cambridge--MA--United-States?page=2">
<span class="screen-reader-only">Next</span><i class="icon icon-caret-right"></i></a>
</li>
</ul>
</div>
</div>
`
func main() {
doc, err := goquery.NewDocumentFromReader(strings.NewReader(html))
pageCounts := doc.Find(".pagination-responsive .list-unstyled")
pageCounts.Each(func(_ int, ul *goquery.Selection) {
links := ul.Find("li a")
links.Each(func(_ int, li *goquery.Selection) {
if val, ok := li.Attr("target"); ok {
fmt.Println(val)
}
})
})
}