• go 解析path


    使用库

    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
    ",htmlquery.InnerText(b))
    	}
    }
    

      

  • 相关阅读:
    centos 查看硬盘使用情况
    查看centos内存命令
    VS2008编译运行时出现“外部组件发生异常”错误的解决方法
    20170307-1
    20170307
    centos7安装配置git
    Git理解笔记3
    Git理解笔记2
    Git理解笔记1
    php-设计模式
  • 原文地址:https://www.cnblogs.com/php-linux/p/13094999.html
Copyright © 2020-2023  润新知