猿问

GoQuery 选择提取器不起作用

我正在尝试从 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">&hellip;</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)

    }

有人可以指出我在这里可能缺少什么吗?


收到一只叮咚
浏览 195回答 1
1回答

眼眸繁星

li := pageIterator.Find("li a")实际上是一个元素序列,但你只需要第一个元素的属性。在这方面,它有点像 jquery。您真正想要做的是遍历所有链接,并Each在这里成为您的朋友。我发现它比用 Eq 迭代要容易得多。这个片段对我有用:var html = `<div class="pagination pagination-responsive">&nbsp; &nbsp; &nbsp; <ul class="list-unstyled">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <li class="active">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<a rel="start" target="1" href="/s/Cambridge--MA--United-States">1</a>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </li>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <li>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<a rel="next" target="2" href="/s/Cambridge--MA--United-States?page=2">2</a>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </li>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <li>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a target="3" href="/s/Cambridge--MA--United-States?page=3">3</a>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </li>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <li class="gap"><span class="gap">&hellip;</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </li>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <li>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a target="17" href="/s/Cambridge--MA--United-States?page=17">17</a>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </li>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <li class="next next_page"><a target="2" rel="next" href="/s/Cambridge--MA--United-States?page=2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="screen-reader-only">Next</span><i class="icon icon-caret-right"></i></a>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </li>&nbsp; &nbsp; &nbsp; &nbsp; </ul>&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; </div>`func main() {&nbsp; &nbsp; doc, err := goquery.NewDocumentFromReader(strings.NewReader(html))&nbsp; &nbsp; pageCounts := doc.Find(".pagination-responsive .list-unstyled")&nbsp; &nbsp; pageCounts.Each(func(_ int, ul *goquery.Selection) {&nbsp; &nbsp; &nbsp; &nbsp; links := ul.Find("li a")&nbsp; &nbsp; &nbsp; &nbsp; links.Each(func(_ int, li *goquery.Selection) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if val, ok := li.Attr("target"); ok {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Println(val)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; })}
随时随地看视频慕课网APP

相关分类

Go
我要回答