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

Pan*_*ko0 3 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>
        <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>
Run Code Online (Sandbox Code Playgroud)

我想以这个数组结束:

------------------------------------
|Row 1, Content 1| Row 1, Content 2|
------------------------------------
|Row 2, Content 1| Row 2, Content 2|
------------------------------------
Run Code Online (Sandbox Code Playgroud)

正如你们所看到的,我只是忽略了内容 3 和 4。

我的提取代码:

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

  doc.Find("table tr td").Each(func(i int, td *goquery.Selection) {
    // ...
  })
}
Run Code Online (Sandbox Code Playgroud)

我试图添加一个控制器编号,它负责忽略<td>我不想转换和调用的

td.NextAll()
Run Code Online (Sandbox Code Playgroud)

但没有运气。你们知道我应该怎么做才能完成它吗?

谢谢。

Pie*_*Pah 5

您可以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)
}
Run Code Online (Sandbox Code Playgroud)

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