使用goquery查找值包含空格的类。

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

Use goquery to find a class whose value contains whitespace

问题

已回答。用户PuerkitoBio通过他的goquery包帮助了我,我相信我不会是唯一一个想知道如何做到这一点的人。(我可以在两天后将此问题标记为“已回答”)

当使用goquery查找html标签的类时,如果类名包含空格,我遇到了一个问题。这里有一个例子:

package main

import (
	"fmt"
	"github.com/PuerkitoBio/goquery"
	"strings"
)

func main() {
	html_code := strings.NewReader(`
	<html>
	    <body>
	        <h1>
	            <span class="text title">Go </span>
	        </h1>
	        <p>
	            <span class="text">totally </span>
	            <span class="post">kicks </span>
	        </p>
	        <p>
	            <span class="text">hacks </span>
	        </p>
	    </body>
	<html>
	`)
	doc, _ := goquery.NewDocumentFromReader(html_code)
}

如果我想找到类名为"text title"的元素,我原以为可以这样做:

doc.Find(".text title").Each(func(i int, s *goquery.Selection) {
	class, _ := s.Attr("class")
	fmt.Println(class, s.Text())
})

但是这样不起作用。(答案如下。)

英文:

Answered. User PuerkitoBio helped me out with his goquery package, and I'm sure I won't be the only one wondering how to do this. (I can mark this question as 'answered' in two days)

When using goquery to find classes of html tags, I hit a problem when the class contains whitespace. Here's an example:

package main

import (
	&quot;fmt&quot;
	&quot;github.com/PuerkitoBio/goquery&quot;
	&quot;strings&quot;
)

func main() {
	html_code := strings.NewReader(`
&lt;html&gt;
    &lt;body&gt;
        &lt;h1&gt;
            &lt;span class=&quot;text title&quot;&gt;Go &lt;/span&gt;
        &lt;/h1&gt;
        &lt;p&gt;
            &lt;span class=&quot;text&quot;&gt;totally &lt;/span&gt;
            &lt;span class=&quot;post&quot;&gt;kicks &lt;/span&gt;
        &lt;/p&gt;
        &lt;p&gt;
            &lt;span class=&quot;text&quot;&gt;hacks &lt;/span&gt;
        &lt;/p&gt;
    &lt;/body&gt;
&lt;html&gt;
    `)
	doc, _ := goquery.NewDocumentFromReader(html_code)
}

If I want to find the class &quot;text title&quot;, I thought I would do this:

doc.Find(&quot;.text title&quot;).Each(func(i int, s *goquery.Selection) {
	class, _ := s.Attr(&quot;class&quot;)
	fmt.Println(class, s.Text())
})

But this doesn't work. (Answer is below.)

答案1

得分: 13

这是我翻译好的内容:

这是我对HTML理解的问题。class=&quot;text title&quot;中的空格表示class有两个值:texttitle。为了使用goquery查找具有多个属性的类,我需要将它们放在一起(没有空格),并在前面加上一个.。像这样:

doc.Find(".text.title").Each(func(i int, s *goquery.Selection) {
    class, _ := s.Attr("class")
    fmt.Println(class, s.Text())
})

或者,如果我只想找到值为title的类,我可以这样做:

doc.Find(".title").Each(func(i int, s *goquery.Selection) {
    class, _ := s.Attr("class")
    fmt.Println(class, s.Text())
})
英文:

It was a problem with my understanding of HTML. The whitespace inside class=&quot;text title&quot; shows that class has two values: text and title. In order to find multiple attributes of a class with goquery, I need to put them side by side (without whitespace) and prefix them with a .. Like this:

doc.Find(&quot;.text.title&quot;).Each(func(i int, s *goquery.Selection) {
	class, _ := s.Attr(&quot;class&quot;)
	fmt.Println(class, s.Text())
})

Or if I ever want to find only the classes with the value title, I would do this:

doc.Find(&quot;.title&quot;).Each(func(i int, s *goquery.Selection) {
	class, _ := s.Attr(&quot;class&quot;)
	fmt.Println(class, s.Text())
})

huangapple
  • 本文由 发表于 2015年1月14日 08:15:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/27933866.html
匿名

发表评论

匿名网友

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

确定