Skip to content

Drelf2018/xpath

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

xpath

使用 tag 获取对应 xpath 元素

使用

package xpath_test

import (
	"fmt"
	"testing"

	"github.com/Drelf2018/xpath"
)

type College struct {
	Name string `xpath:"./td[1]//span/text()"`
	URL  string `xpath:"./td[2]//a/@href"`
}

func (c College) String() string {
	return fmt.Sprintf("%s(%s)", c.Name, c.URL)
}

type Colleges []College

func (Colleges) XPath() string {
	return "//*[@id='vsb_content']/div/table/tbody/tr[position() > 1]"
}

func TestColleges(t *testing.T) {
	colleges, err := xpath.LoadURLWith[Colleges]("https://gs.dlut.edu.cn/info/1173/14056.htm")
	if err != nil {
		t.Fatal(err)
	}
	for _, college := range colleges {
		t.Log(college)
	}
}

About

使用 tag 获取对应 xpath 元素

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages