goquery

例子

aa.html

<html>
  <body>
    <div id="aaa">
      <ul>
        <li>
          <h1>qqq</h1>
        </li>
        <li>
          <h1>ttt</h1>
        </li>
      </ul>
    </div>

    <div class="bbb">
      <ul>
        <li>
          <h1>zfff</h1>
        </li>
        <li>
          <h1>aaaaa</h1>
        </li>
      </ul>
    </div>
  </body>
</html>

 

go代码

package main

import (
    "fmt"
    "log"
    "github.com/PuerkitoBio/goquery"
)

func myTest()  {
    doc, err := goquery.NewDocument("http://127.0.0.1/aa.html")
    if err != nil {
        log.Fatal(err)
    }

    doc.Find("#aaa ul li").Each(func(i int, node *goquery.Selection) {
        info := node.Find("h1").Text()
        fmt.Println(info)
    })

    doc.Find(".bbb ul li").Each(func(i int, node *goquery.Selection) {
        info := node.Find("h1").Text()
        fmt.Println(info)
    })
}

func main() {
    myTest()
}