如何使用 golang 将 HTML table 转换为数组
How to convert HTML table to array with golang
我在尝试将 HTML table 转换为 Golang 数组时遇到问题。我尝试使用 x/net/html 和 goquery 来实现它,但两者都没有成功。
假设我们有这个 HTML table:
<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()
但运气不好。你们知道我应该怎么做才能完成它吗?
谢谢。
您只能使用软件包 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 模式编写,但将其重构为更通用的模式并不难。
尝试这样的方法来制作二维数组并处理可变行大小:
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)
}
我在尝试将 HTML table 转换为 Golang 数组时遇到问题。我尝试使用 x/net/html 和 goquery 来实现它,但两者都没有成功。
假设我们有这个 HTML table:
<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()
但运气不好。你们知道我应该怎么做才能完成它吗?
谢谢。
您只能使用软件包 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 模式编写,但将其重构为更通用的模式并不难。
尝试这样的方法来制作二维数组并处理可变行大小:
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)
}