为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

Go
牛魔王的故事 2021-12-20 16:53:54
我在尝试将 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>我想以这个数组结束:------------------------------------|Row 1, Content 1| Row 1, Content 2|------------------------------------|Row 2, Content 1| Row 2, Content 2|------------------------------------正如你们所看到的,我只是忽略了内容 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>我不想转换和调用的td.NextAll()但没有运气。你们知道我应该怎么做才能完成它吗?
查看完整描述

2 回答

?
翻翻过去那场雪

TA贡献2065条经验 获得超14个赞

您可以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)

}

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


查看完整回答
反对 回复 2021-12-20
?
繁星点点滴滴

TA贡献1803条经验 获得超3个赞

尝试这样的方法来制作二维数组并处理可变行大小:


    z := html.NewTokenizer(body)

    table := [][]string{}

    row := []string{}


    for z.Token().Data != "html" {

        tt := z.Next()

        if tt == html.StartTagToken {

            t := z.Token()


            if t.Data == "tr" {

                if len(row) > 0 {

                    table = append(table, row)

                    row = []string{}

                }

            }


            if t.Data == "td" {

                inner := z.Next()


                if inner == html.TextToken {

                    text := (string)(z.Text())

                    t := strings.TrimSpace(text)

                    row = append(row, t)

                }

            }


        }

    }

    if len(row) > 0 {

        table = append(table, row)

    }


查看完整回答
反对 回复 2021-12-20
  • 2 回答
  • 0 关注
  • 249 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号