使用库
https://github.com/antchfx/htmlquery
package main
import (
"fmt"
"github.com/antchfx/htmlquery"
"golang.org/x/net/html"
"golang.org/x/net/html/charset"
"net/http"
)
func main() {
url := "http://tool.yeves.cn"
client := &http.Client{}
req,err := http.NewRequest("GET",url,nil)
if err != nil{
fmt.Println(err)
}
req.Header.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.108 Safari/537.2222")
req.Header.Add("testet-sdfsdf","english")
resp,err := client.Do(req)
defer resp.Body.Close()
r, err := charset.NewReader(resp.Body, resp.Header.Get("Content-Type"))
if err != nil {
return
}
content,_ := html.Parse(r)
parse(content)
}
func parse(doc *html.Node) {
list, err := htmlquery.QueryAll(doc, "//form//div")
if err != nil {
fmt.Println(err)
}
for i, n := range list {
a := htmlquery.FindOne(n, "//label")
fmt.Printf("%d %s", i, htmlquery.InnerText(a))
b := htmlquery.FindOne(n,"//input/@placeholder")
fmt.Printf(" %s\n",htmlquery.InnerText(b))
}
}
来源:oschina
链接:https://my.oschina.net/u/4368375/blog/4308490