如何用golang将HTML表转换为数组

如何用golang将HTML表转换为数组,html,go,goquery,Html,Go,Goquery,我在尝试将HTML表转换为Golang数组时遇到问题。我尝试使用x/net/html和goquery来实现它,但在这两个方面都没有成功 假设我们有这个HTML表: <html> <body> <table> <tr> <td>Row 1, Content 1</td> <td>Row 1, Content 2</td> <t

我在尝试将HTML表转换为Golang数组时遇到问题。我尝试使用x/net/html和goquery来实现它,但在这两个方面都没有成功

假设我们有这个HTML表:

<html>
  <body>
    <table>
      <tr>
        <td>Row 1, Content 1</td>
        <td>Row 1, Content 2</td>
        <td>Row 1, Content 3</td>
        <td>Row 1, Content 4</td>
      </tr>
      <tr>
        <td>Row 2, Content 1</td>
        <td>Row 2, Content 2</td>
        <td>Row 2, Content 3</td>
        <td>Row 2, Content 4</td>
      </tr>
    </table>
  </body>
</html>
正如你们所看到的,我只是忽略了内容3和内容4

我的提取代码:

func extractValue(content []byte) {
  doc, _ := goquery.NewDocumentFromReader(bytes.NewReader(content))

  doc.Find("table tr td").Each(func(i int, td *goquery.Selection) {
    // ...
  })
}
我试图添加一个控制器号码,它将负责忽略我不想转换和调用的

td.NextAll()
但是没有运气。你们知道我该怎么做才能完成吗


谢谢。

您只需使用package
golang.org/x/net/html
即可

var body = strings.NewReader(`                                                                                                                            
        <html>                                                                                                                                            
        <body>                                                                                                                                            
        <table>                                                                                                                                           
        <tr>                                                                                                                                              
        <td>Row 1, Content 1</td>                                                                                                                          
        <td>Row 1, Content 2</td>                                                                                                                          
        <td>Row 1, Content 3</td>                                                                                                                          
        <td>Row 1, Content 4</td>                                                                                                                          
        </tr>                                                                                                                                             
        <tr>                                                                                                                                              
        <td>Row 2, Content 1</td>                                                                                                        
        <td>Row 2, Content 2</td>                                                                                                                          
        <td>Row 2, Content 3</td>                                                                                                                          
        <td>Row 2, Content 4</td>                                                                                                                          
        </tr>  
        </table>                                                                                                                                          
        </body>                                                                                                                                           
        </html>`)          

func main() {
    z := html.NewTokenizer(body)
    content := []string{}

    // While have not hit the </html> tag
    for z.Token().Data != "html" {
        tt := z.Next()
        if tt == html.StartTagToken {
            t := z.Token()
            if t.Data == "td" {
                inner := z.Next()
                if inner == html.TextToken {
                    text := (string)(z.Text())
                    t := strings.TrimSpace(text)
                    content = append(content, t)
                }
            }
        }
    }
    // Print to check the slice's content
    fmt.Println(content)
}
var body=strings.NewReader(`
第1行,内容1
第1行,内容2
第1行,内容3
第1行,内容4
第2行,内容1
第2行,内容2
第2行,内容3
第2行,内容4
`)          
func main(){
z:=html.NewTokenizer(body)
内容:=[]字符串{}
//虽然还没有达到目标
对于z.Token().Data!=“html”{
tt:=z.Next()
如果tt==html.StartTagToken{
t:=z.Token()
如果t.数据==“td”{
内部:=z.Next()
如果内部==html.TextToken{
text:=(字符串)(z.text())
t:=strings.TrimSpace(文本)
content=append(content,t)
}
}
}
}
//打印以检查切片的内容
fmt.Println(内容)
}

此代码仅针对这种典型的HTML模式编写,但将其重构为更通用的模式并不困难。

尝试这样的方法来创建2d数组并处理可变的行大小:

z:=html.NewTokenizer(主体)
表:=[]字符串{}
行:=[]字符串{}
对于z.Token().Data!=“html”{
tt:=z.Next()
如果tt==html.StartTagToken{
t:=z.Token()
如果t.数据==“tr”{
如果len(世界其他地区)>0{
表=追加(表,行)
行=[]字符串{}
}
}
如果t.数据==“td”{
内部:=z.Next()
如果内部==html.TextToken{
text:=(字符串)(z.text())
t:=strings.TrimSpace(文本)
行=追加(行,t)
}
}
}
}
如果len(世界其他地区)>0{
表=追加(表,行)
}

您能添加实际使用的代码吗?html表格看起来无效。如果我没弄错的话,这里没有结束td标签。非常感谢。在修改了一点代码后,我得到了预期的结果@DanilloSouza很乐意帮忙!
var body = strings.NewReader(`                                                                                                                            
        <html>                                                                                                                                            
        <body>                                                                                                                                            
        <table>                                                                                                                                           
        <tr>                                                                                                                                              
        <td>Row 1, Content 1</td>                                                                                                                          
        <td>Row 1, Content 2</td>                                                                                                                          
        <td>Row 1, Content 3</td>                                                                                                                          
        <td>Row 1, Content 4</td>                                                                                                                          
        </tr>                                                                                                                                             
        <tr>                                                                                                                                              
        <td>Row 2, Content 1</td>                                                                                                        
        <td>Row 2, Content 2</td>                                                                                                                          
        <td>Row 2, Content 3</td>                                                                                                                          
        <td>Row 2, Content 4</td>                                                                                                                          
        </tr>  
        </table>                                                                                                                                          
        </body>                                                                                                                                           
        </html>`)          

func main() {
    z := html.NewTokenizer(body)
    content := []string{}

    // While have not hit the </html> tag
    for z.Token().Data != "html" {
        tt := z.Next()
        if tt == html.StartTagToken {
            t := z.Token()
            if t.Data == "td" {
                inner := z.Next()
                if inner == html.TextToken {
                    text := (string)(z.Text())
                    t := strings.TrimSpace(text)
                    content = append(content, t)
                }
            }
        }
    }
    // Print to check the slice's content
    fmt.Println(content)
}