42 lines
842 B
Markdown
42 lines
842 B
Markdown
# ptrcnull/html
|
|
|
|
library wrapping `net/html` with a bunch of convenience functions
|
|
|
|
example usage:
|
|
```go
|
|
package main
|
|
|
|
import (
|
|
"log"
|
|
"os"
|
|
|
|
"git.ddd.rip/ptrcnull/html"
|
|
)
|
|
|
|
func main() {
|
|
htmlText := getSomeHtmlPage()
|
|
document, err := html.ParseDocument(htmlText)
|
|
if err != nil {
|
|
log.Fatalf("parse document: %w\n", err)
|
|
}
|
|
|
|
table := document.QuerySelector("table")
|
|
if table == nil {
|
|
log.Fatalln("table not found")
|
|
}
|
|
|
|
if os.Getenv("DEBUG") == "1" {
|
|
log.Println("table:", table.Render())
|
|
}
|
|
|
|
for _, row := range table.QuerySelectorAll("tr") {
|
|
if strings.Contains(row.Text(), "a thing you're looking for") {
|
|
cell := row.FindOne(func(n *html.Node) {
|
|
return n.HasAttr("aria-label") && n.GetAttr("aria-label")[0] == "Value"
|
|
})
|
|
|
|
log.Println("value:", cell.TrimmedText())
|
|
}
|
|
}
|
|
}
|
|
``` |