0

How to select an image (or another HTML tag) with XPath in Go?

resp, _ := http.Get(url)
bytes, _ := ioutil.ReadAll(resp.Body)

s := string(bytes))

how to parse s with XPath?

like this code:

list := libxxxx.Find(s, "//a@href")

I get HTML code with http.Get but when I want to parse it I have a problem.

2
  • share your full code what your html contains and what are you want to get after parsing the html. Commented Oct 5, 2018 at 10:21
  • 1
    you can also check out this link Commented Oct 5, 2018 at 10:23

1 Answer 1

4

you can use htmlquery:

doc, err := htmlquery.LoadURL("http://example.com/")

or use string:

s := `<html>....</html>`
doc, err := htmlquery.Parse(strings.NewReader(s))

then find everything:

list := htmlquery.Find(doc, "//a")
list := range htmlquery.Find(doc, "//a[@href]") 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.