go xpath 添加头部解析

package main

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

func getResponse(url string ) *http.Response {

	client := &http.Client{}
	//生成要访问的url
	//提交请求
	request, err := http.NewRequest("GET", url, nil)

	//增加header选项
	request.Header.Add("User-Agent", "golang")

	if err != nil {
		panic(err)
	}
	//处理返回结果
	resp, _ := client.Do(request)

	return resp
}

func main() {
	var url string = "https://www.cnblogs.com/brady-wang/"
	response := getResponse(url)
	defer response.Body.Close()

	doc,_ := htmlquery.Parse(response.Body)
	list := htmlquery.Find(doc, "//*[@id='mainContent']//div[@class='postTitle']/a")
	for _,item := range list{
		title := htmlquery.InnerText(item)
		title = strings.Replace(title, " ", "", -1)
		// 去除换行符
		title = strings.Replace(title, "
", "", -1)
		fmt.Printf("title %s",title)
		fmt.Printf("href %s
",htmlquery.SelectAttr(item,"href"))
	}

}
原文地址:https://www.cnblogs.com/brady-wang/p/14307868.html