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, "\n", "", -1)
		fmt.Printf("title %s",title)
		fmt.Printf("href %s\n",htmlquery.SelectAttr(item,"href"))
	}

}

相关文章:

  • 2021-04-17
  • 2021-11-17
  • 2021-07-08
  • 2022-12-23
  • 2021-11-30
  • 2021-06-08
  • 2021-07-24
  • 2021-04-19
猜你喜欢
  • 2022-12-23
  • 2021-11-11
  • 2022-01-17
  • 2021-06-21
  • 2022-12-23
  • 2021-12-05
  • 2021-10-30
相关资源
相似解决方案