Skip to content

Releases: Aiicy/htmlquery

Burn version to v2.0.0

02 Dec 14:41
f99bb02
Compare
Choose a tag to compare

主要是添加context 支持
使用例子:

package main
import (
	"fmt"
	"context"

	"github.com/Aiicy/htmlquery"
)

func main() {
    ctx := context.Background()
    ctx, cancel := context.WithTimeout(ctx, time.Second) //设置超时时间为 1秒
    defer cancel()
	doc, err := htmlquery.LoadURL(ctx,"https://www.bing.com/search?q=golang")
	if err != nil {
		panic(err)
	}
	// Find all news item.
	for i, n := range htmlquery.Find(doc, "//ol/li") {
		a := htmlquery.FindOne(n, "//a")
		fmt.Printf("%d %s(%s)\n", i, htmlquery.InnerText(a), htmlquery.SelectAttr(a, "href"))
	}
}