1

I would like to retrieve all the tags with a specific id using Go. Apparently the easiest way to do that is go is to use goquery.

Assume I am looking for ul tags with id MyTag in a website site. I would like to list all the li contained in such a ul. I never used jQuery before so I feel a little lost.

resp, _ := http.Get(site)
httpBody := resp.Body
node, _ := html.Parse(httpBody)
document := goquery.NewDocumentFromNode(node)
document.Find("ul.MyTag").Each(func(i int, ul *goquery.Selection) { //MyTag will not work here
    ul.Find("li").Each(func (i int, li *goquery.Selection){
        ...
   })
})

More specitically, my html looks like

<html>
    <body>
        <ui id="yes">
            <li key="1">a</li>
            <li key="2">b</li>
            <li key="3">c</li>
            <li key="4">d</li>
        </ui>

        <ui id="no">
            <li key="1">11</li>
            <li key="2">22</li>
            <li key="3">33</li>
            <li key="4">44</li>
        </ui>
    </body>
</html>

and I would like to retreive the keys 1,2,3,4

Bonus question: why Each has an int argument? It doesn't seem to be used at all

4

1 回答 1

8

GoQuery 使用与 jQuery/CSS 相同的选择器语法。为此,如果您想查找具有特定 ID 的元素 .. 那么您需要使用井#号。

document.Find("ul#MyTag")...

话虽如此,ID应该是唯一的。您正在使用的上述代码(我在您的上一个问题中提供).. 按class(点.符号)搜索。

向我们展示您正在使用它的标记,我将能够准确地看到您出错的地方。

RE:你的奖金问题。int参数是元素在其父元素中的索引。您不必使用它.. 它是由 goquery 提供的。

于 2014-10-02T00:56:53.847 回答