xpath

package module
v0.0.0-...-265e037 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 28, 2025 License: MIT Imports: 10 Imported by: 0

README

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)
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func LoadDoc

func LoadDoc(path string, v any) error

func LoadDocWith

func LoadDocWith[V any](path string) (v V, err error)

func LoadURL

func LoadURL(url string, v any) error

func LoadURLWith

func LoadURLWith[V any](url string) (v V, err error)

func TypePtr

func TypePtr(in any) uintptr

func Unmarshal

func Unmarshal(b []byte, v any) error

func UnmarshalNode

func UnmarshalNode(node *html.Node, v any) error

func UnmarshalNodeWith

func UnmarshalNodeWith[V any](node *html.Node) (v V, err error)

func UnmarshalReader

func UnmarshalReader(r io.Reader, v any) error

func UnmarshalReaderWith

func UnmarshalReaderWith[V any](r io.Reader) (v V, err error)

func UnmarshalText

func UnmarshalText(text string, v any) error

func UnmarshalTextWith

func UnmarshalTextWith[V any](text string) (v V, err error)

func UnmarshalWith

func UnmarshalWith[V any](b []byte) (v V, err error)

func ValuePtr

func ValuePtr(in any) uintptr

ValuePtr can obtain the uintptr of a type from its reflect.Type

TypePtr(something{}) is equal to ValuePtr(reflect.TypeOf(something{}))

Types

type Any

type Any struct {
	Type  unsafe.Pointer
	Value unsafe.Pointer
}

type Expr

type Expr struct {
	Index int
	*xpath.Expr
}

func GetExpr

func GetExpr(elem reflect.Type) ([]*Expr, error)

func LoadExpr

func LoadExpr(v any) ([]*Expr, error)

type XPath

type XPath interface {
	XPath() string
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL