0
点赞
收藏
分享

微信扫一扫

go xpath

河南妞 2022-03-11 阅读 25





​​

​​

​​


​​

​​

​​






package main

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

func main() {
url := "http://quotes.toscrape.com/"

req, _ := http.NewRequest("GET", url, nil)
req.Header.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3776.0 Safari/537.36")
client := &http.Client{Timeout: time.Second * 5}
resp, err := client.Do(req)
if err != nil {
log.Fatalln(err)
}
defer resp.Body.Close()

doc,_ := htmlquery.Parse(resp.Body)
list := htmlquery.Find(doc, "//div[@class=\"quote\"]")

for _,n := range list {
content := htmlquery.FindOne(n,".//span[1]")
author := htmlquery.FindOne(n,"/span[2]//small")

fmt.Printf("%s-%s\n",htmlquery.InnerText(author), htmlquery.InnerText(content))

}

}





举报

相关推荐

0 条评论