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
    

      

  • 相关阅读:
    PCL点云
    unity碰撞,刚体笔记
    动画剪辑 状态配置 脚本编写2
    unity中动画剪辑,状态机关系配置,脚本编写方式1
    unity给物体着色加纹理图 material(材质)
    C++/Java小白解Leetcode题,发现了知识盲区……
    NLP之统计句法分析(PCFG+CYK算法)
    Java:基于TCP协议网络socket编程(实现C/S通信)
    Java实现:抛开jieba等工具,写HMM+维特比算法进行词性标注
    Java多线程技术:实现多用户服务端Socket通信
  • 原文地址:https://www.cnblogs.com/brady-wang/p/13554628.html
Copyright © 2011-2022 走看看