go 解析path

戏子无情 提交于 2020-10-28 00:03:27

使用库

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))
	}
}

  

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!