-1

if there's a html like this:

<select name="" id="selectxzq">
    <option value="">-chose-one-</option>
    <option value="44">A-chose</option>
    <option value="14">B-chose</option>
    <option value="39">C-chose</option>
    <option value="4">D-chose</option>
</select>

when i write code like this:

...
    body, err := ioutil.ReadAll(resp.Body)
    rspHtml := mahonia.NewDecoder("UTF-8").ConvertString(string(body))
    doc, err := goquery.NewDocumentFromReader(strings.NewReader(rspHtml))
    if err != nil {
        log.Fatalln(err)
    }
    // Find the review items
    doc.Find("#selectxzq").Each(func(i int, s *goquery.Selection) {
        optionT := s.Find("option").Text()
        fmt.Printf("Review %d:  %s  \n", i, optionT)
    })

I also can get the text() of the options,like this:

Review 0: -chose-one-A-choseB-choseC-choseD-chose

but I don't konw how to get the value of the option by use the goquery,the .Attr() methods also can not use to get the value

nil false
  • 1
  • 1
  • 1
    Does this answer your question? [how to get value of attribute href value in Go language.](https://stackoverflow.com/questions/32171498/how-to-get-value-of-attribute-href-value-in-go-language) – Riwen Apr 06 '21 at 16:31

2 Answers2

0

you should also iterate the options.

    doc.Find("#selectxzq").Each(func(i int, s *goquery.Selection) {
       s.Find("option").Each(func(i int, selection *goquery.Selection) {
         fmt.Printf("Review %d:  %s %s \n", i,selection.AttrOr("value",""), selection.Text())
    })
})
ahmetlutfu
  • 325
  • 2
  • 9
0

at last , for get the value i use this way

val, exists := option.Attr("value")

point is , there's a exists and input the val of element in the Attr() methods.

tks!

nil false
  • 1
  • 1