Search tags by id with goQuery

huangapple go评论79阅读模式
英文:

Search tags by id with goQuery

问题

我想使用Go语言检索具有特定id的所有标签。显然,在Go中最简单的方法是使用goquery。

假设我正在查找网站site中具有id为MyTagul标签。我想列出该ul中包含的所有li标签。我以前从未使用过jQuery,所以感到有点迷茫。

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) {
    ul.Find("li").Each(func(i int, li *goquery.Selection) {
        ...
    })
})

更具体地说,我的HTML如下所示:

<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>

我想要获取键1,2,3,4

额外问题:为什么Each函数有一个int参数?它似乎根本没有被使用。

英文:

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(&quot;ul.MyTag&quot;).Each(func(i int, ul *goquery.Selection) { //MyTag will not work here
	ul.Find(&quot;li&quot;).Each(func (i int, li *goquery.Selection){
        ...
   })
})

More specitically, my html looks like

&lt;html&gt;
    &lt;body&gt;
        &lt;ui id=&quot;yes&quot;&gt;
            &lt;li key=&quot;1&quot;&gt;a&lt;/li&gt;
            &lt;li key=&quot;2&quot;&gt;b&lt;/li&gt;
            &lt;li key=&quot;3&quot;&gt;c&lt;/li&gt;
            &lt;li key=&quot;4&quot;&gt;d&lt;/li&gt;
        &lt;/ui&gt;

        &lt;ui id=&quot;no&quot;&gt;
            &lt;li key=&quot;1&quot;&gt;11&lt;/li&gt;
            &lt;li key=&quot;2&quot;&gt;22&lt;/li&gt;
            &lt;li key=&quot;3&quot;&gt;33&lt;/li&gt;
            &lt;li key=&quot;4&quot;&gt;44&lt;/li&gt;
        &lt;/ui&gt;
    &lt;/body&gt;
&lt;/html&gt;

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

答案1

得分: 8

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

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

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

请向我们展示您在其中使用此代码的标记,我将能够准确地看到您出了什么问题。

关于您的奖励问题。int参数是元素在其父级中的索引。您不一定要使用它..不过goquery提供了它。

英文:

GoQuery uses the same selector syntax as jQuery/CSS. To that end, if you want to find an element with a specific ID .. then you need to use a hash # symbol.

document.Find(&quot;ul#MyTag&quot;)...

That being said, ID's are supposed to be unique. The above code you're using (that I provided in a previous question of yours) .. searches by class (the dot . notation).

Show us the markup you're using this on and I will be able to see exactly where you're going wrong.

RE: Your bonus question. The int argument is the index of the element within its parent. You don't have to use it.. it is provided by goquery though.

huangapple
  • 本文由 发表于 2014年10月2日 08:28:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/26152766.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定