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
    

      

  • 相关阅读:
    只要7步,就能将任何魔方6面还原
    写一篇文章测试一下
    关于80端口被占用
    打造只能输入数字的文本框
    windows下MySql忘记密码的解决方案
    linq to xml 操作sitemap
    C#设计模式——工厂方法模式(Factory Method Pattern)
    C#设计模式——单件模式(Singleton Pattern)
    C#设计模式——迭代器模式(Iterator Pattern)
    C#设计模式——状态模式(State Pattern)
  • 原文地址:https://www.cnblogs.com/brady-wang/p/13554628.html
Copyright © 2011-2022 走看看