antchfx / htmlquery

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

A bug when I use htmlquery.InnerText #38

Closed pgshow closed 3 years ago

pgshow commented 3 years ago

htmlquery.InnerText(htmlquery.FindOne(n, "//td[2]"))

In this code, if htmlquery.FindOne(n, "//td[2]") is nil, so htmlquery.InnerText will throw out an error, which is not convincing. Better to return a nil too.

zhengchun commented 3 years ago

Hello, you should check htmlquery.FindOne(n, "//td[2]") return value whether is nil before call htmlquery.InnerText. if n:=htmlquery.FindOne(n, "//td[2]");n!=nil{fmt.Println(htmlquery.InnerText(n)}

another, htmlquery.InnerText(htmlquery.FindOne(n, "//td[2]")) return "" instead of panic will cause confusing we don't know there is not find node or found node that it's value is empty.

pgshow commented 3 years ago

Ok, I will always check nil, for InnerText