zoukankan      html  css  js  c++  java
  • 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
    ",htmlquery.InnerText(author), htmlquery.InnerText(content))
    
        }
    
    }

    结果

    GOROOT=C:Go #gosetup
    GOPATH=E:wwwgopath #gosetup
    C:Goingo.exe build -o C:UsersAdministratorAppDataLocalTemp\___go_build_main_go.exe E:wwwgomain.go #gosetup
    C:UsersAdministratorAppDataLocalTemp\___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
    

      

  • 相关阅读:
    vue中处理ie兼容性问题
    vue使用websocket
    vue-cli中使用sass的坑
    really_probe()
    ro.boot.bootreason property设置(androidboot.xxxx bootargs)
    kernel exception vector table
    compile/link misc
    user space syscall/library API misc
    LIUNX SHELL中-a 到-z的解释
    getenforce/setenforce
  • 原文地址:https://www.cnblogs.com/brady-wang/p/13554628.html
Copyright © 2011-2022 走看看