antchfx / htmlquery

htmlquery is golang XPath package for HTML query.
https://github.com/antchfx/xpath
MIT License
723 stars 73 forks source link

The example fails in ubuntu 20.04 #47

Closed GermanG closed 2 years ago

GermanG commented 2 years ago

Trying to use FindOne (and failing) I ended up testing the example as in the README.md

~/golang $ cat example.go
package main

import (
    "fmt"
    "github.com/antchfx/htmlquery"
)
 func main() {
    doc, err := htmlquery.LoadURL("https://www.bing.com/search?q=golang")
    if err != nil {
        panic(err)
    }
    // Find all news item.
    list, err := htmlquery.QueryAll(doc, "//ol/li")
    if err != nil {
        panic(err)
    }
    for i, n := range list {
        a := htmlquery.FindOne(n, "//a")
        fmt.Printf("%d %s(%s)\n", i, htmlquery.InnerText(a), htmlquery.SelectAttr(a, "href"))
    }
}
 ~/golang $ go run example.go
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x28 pc=0x6ae4aa]

goroutine 1 [running]:
github.com/antchfx/htmlquery.InnerText.func1(0xc000132ae0, 0x0)
    /home/user/go/src/github.com/antchfx/htmlquery/query.go:148 +0x2a
github.com/antchfx/htmlquery.InnerText(0x0, 0x75b52e, 0x3)
    /home/user/go/src/github.com/antchfx/htmlquery/query.go:161 +0x84
main.main()
    /home/user/golang/example.go:19 +0xc8
exit status 2
go version
go version go1.13.8 linux/amd64

Any hints?

Thanks!

zhengchun commented 2 years ago

There is no promise that Bing will not change the page structure in the future. You just change a little code to solve this.

for i, n := range list {
    a := htmlquery.FindOne(n, "//a")
    if a!= nil {
        fmt.Printf("%d %s(%s)\n", i, htmlquery.InnerText(a), htmlquery.SelectAttr(a, "href"))
    }
}