Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Does it support "string" xpath function? #37

Closed
Nillouise opened this issue Jan 10, 2021 · 2 comments
Closed

Does it support "string" xpath function? #37

Nillouise opened this issue Jan 10, 2021 · 2 comments

Comments

@Nillouise
Copy link

I want to use
htmlquery.Query(parse, "string(//div[@id='postlist'])")
to extract the text content, but it return nil. It seem it does not support the "string" xpath function?

@zhengchun
Copy link
Contributor

Yes, supports string , you can check xpath document.

string is a function that return string value, this means you can not call htmlquery.Query, htmlquery.Query is used for query node-set. you should use Evaluate method, like this:

expr, _ := xpath.Compile("count(//img)")
v := expr.Evaluate(htmlquery.CreateXPathNavigator(doc)).(float64)
fmt.Printf("total count is %f", v)

another ways is use text() on your xpath query. like this htmlquery.Query(parse, "//div[@id='postlist']/text()")

@Nillouise
Copy link
Author

Thank for you reply, now it work with:

			response, err := getRequest(link)
			if err != nil {
				fmt.Println("error ", err)
				return
			}
			parse, err := htmlquery.Parse(response.Body)
			if err != nil {
				fmt.Println("error ", err)
				return
			}
			expr, _ := xpath.Compile("string(//div[@id='postlist'])")
			b := expr.Evaluate(htmlquery.CreateXPathNavigator(parse)).(string)
                        fmt.Printf("total count is %s", b)

but it seem cannot process the utf8 character, the return string have messy code, but doesn't matter.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants