英文:
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 (
"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)
}
If I want to find the class "text title"
, I thought I would do this:
doc.Find(".text title").Each(func(i int, s *goquery.Selection) {
class, _ := s.Attr("class")
fmt.Println(class, s.Text())
})
But this doesn't work. (Answer is below.)
答案1
得分: 13
这是我翻译好的内容:
这是我对HTML理解的问题。class="text title"
中的空格表示class
有两个值:text
和title
。为了使用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="text title"
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(".text.title").Each(func(i int, s *goquery.Selection) {
class, _ := s.Attr("class")
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(".title").Each(func(i int, s *goquery.Selection) {
class, _ := s.Attr("class")
fmt.Println(class, s.Text())
})
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论