zoukankan      html  css  js  c++  java
  • go语言使用xpath

    1.导包

     gopm get -g -v github.com/lestrrat-go/libxml2
    
    

    2.使用示例

    func ExampleHTML() {
      res, err := http.Get("http://golang.org")
      if err != nil {
        panic("failed to get golang.org: " + err.Error())
      }
    
      doc, err := libxml2.ParseHTMLReader(res.Body)
      if err != nil {
        panic("failed to parse HTML: " + err.Error())
      }
      defer doc.Free()
    
      doc.Walk(func(n types.Node) error {
        log.Printf(n.NodeName())
        return nil
      })
    
      nodes := xpath.NodeList(doc.Find(`//div[@id="menu"]/a`))
      for i := 0; i < len(nodes); i++ {
        log.Printf("Found node: %s", nodes[i].NodeName())
      }
    }
    

    //bytes[]转io.Reader()的例子

    package parser
    
    import (
    	"bytes"
    	"fmt"
    	"github.com/lestrrat-go/libxml2"
    	"goproject/crawler/Fetcher"
    	"testing"
    )
    
    func TestParseCityList(t *testing.T) {
    	contents, err := Fetcher.Fetch("http://www.zhenai.com/zhenghun")
    	if err != nil {
    		panic(err)
    	}	
    	//bytes[]转io.Reader()
    	doc, err := libxml2.ParseHTMLReader(bytes.NewReader(contents))
    	defer doc.Free()
    	nodes, err := doc.Find("//dl[@class='city-list clearfix']/dd/a")
    	fmt.Println(nodes.NodeList()[0].TextContent(), err)
    }
    
    
  • 相关阅读:
    上下界网络流——概念解析与快速入门(待修改)
    maomao的现在与未来
    exgcd证明和最基础应用
    快速入门Splay
    luogu 2515
    bzoj 1996
    *51nod 1409
    51nod 1412
    51nod 1503
    51nod 1020
  • 原文地址:https://www.cnblogs.com/c-x-a/p/10787962.html
Copyright © 2011-2022 走看看