go xpath 百度

package main

import (
	"fmt"
	"github.com/antchfx/htmlquery"
	"net/http"
)

func main() {

	var url string = "https://www.baidu.com/"

	client := &http.Client{}
	requestGet, _:= http.NewRequest("GET", url, nil)
	requestGet.Header.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36")
	resp, err := client.Do(requestGet)
	if err != nil {
		fmt.Printf("get request failed, err:[%s]", err.Error())
		return
	}
	defer resp.Body.Close()

	doc,_ := htmlquery.Parse(resp.Body)
	list := htmlquery.Find(doc, "//*[@id=\"su\"]")
	for _,li := range list{
		fmt.Printf("%s\n",htmlquery.SelectAttr(li,"value"))
	}
}

  

posted @ 2021-11-23 16:23  brady-wang  阅读(62)  评论(0编辑  收藏  举报