golang xpath解析网页

https://github.com/antchfx/htmlquery

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

    }

}

 

结果

GOROOT=C:\Go #gosetup
GOPATH=E:\www\gopath #gosetup
C:\Go\bin\go.exe build -o C:\Users\Administrator\AppData\Local\Temp\___go_build_main_go.exe E:\www\go\main.go #gosetup
C:\Users\Administrator\AppData\Local\Temp\___go_build_main_go.exe #gosetup
Albert Einstein-“The world as we have created it is a process of our thinking. It cannot be changed without changing our thinking.”
J.K. Rowling-“It is our choices, Harry, that show what we truly are, far more than our abilities.”
Albert Einstein-“There are only two ways to live your life. One is as though nothing is a miracle. The other is as though everything is a miracle.”
Jane Austen-“The person, be it gentleman or lady, who has not pleasure in a good novel, must be intolerably stupid.”
Marilyn Monroe-“Imperfection is beauty, madness is genius and it's better to be absolutely ridiculous than absolutely boring.”
Albert Einstein-“Try not to become a man of success. Rather become a man of value.”
André Gide-“It is better to be hated for what you are than to be loved for what you are not.”
Thomas A. Edison-“I have not failed. I've just found 10,000 ways that won't work.”
Eleanor Roosevelt-“A woman is like a tea bag; you never know how strong it is until it's in hot water.”
Steve Martin-“A day without sunshine is like, you know, night.”

Process finished with exit code 0

  

上一篇:[ML] Cost Function


下一篇:Autopilot Pattern Applications 开发模式