Golang读取HTML中Table数据到二维数组
作者:互联网
利用goquery读取html中指定ID的数据保存至二维数组中。
代码如下:
1 package main 2 3 import ( 4 "fmt" 5 "log" 6 "strings" 7 "io" 8 9 "github.com/PuerkitoBio/goquery" 10 ) 11 12 func main() { 13 html := `<html> 14 <body> 15 <table id="mydata"> 16 <tr id="one"> 17 <td>one</td> 18 <td>two</td> 19 <td>three</td> 20 </tr> 21 <tr id="two"> 22 <td>3</td> 23 <td>4</td> 24 <td>5</td> 25 </tr> 26 <tr id="three"> 27 <td>八</td> 28 <td>九</td> 29 <td>十</td> 30 </tr> 31 <tr id="four"> 32 <td>百</td> 33 <td>千</td> 34 <td>万</td> 35 </tr> 36 <tr id="five"> 37 <td>100</td> 38 <td>200</td> 39 <td>300</td> 40 </tr> 41 </table> 42 </body> 43 </html> 44 ` 45 46 var table [][]string = GetHtmlTableData(strings.NewReader(html), "#mydata") 47 48 fmt.Println(table) 49 50 for trkey,tr := range table { 51 for tdkey, _ := range tr{ 52 fmt.Println(table[trkey][tdkey]) 53 } 54 } 55 } 56 57 func GetHtmlTableData(html io.Reader, tableId string) (table [][]string) { 58 var tableCache [][]string 59 60 dom,err:=goquery.NewDocumentFromReader(html) 61 if err != nil{ 62 log.Fatalln(err) 63 } 64 65 trNum := dom.Find("table"+tableId).Find("tr").Length() 66 tdNum := dom.Find("table"+tableId).Find("tr").Find("td").Length() 67 68 tableCache = make([][]string, trNum, trNum) 69 for i:=0; i<trNum; i++{ 70 tableCache[i] = make([]string, tdNum/trNum, tdNum/trNum) 71 for j:=0; j<(tdNum/trNum); j++ { 72 tableCache[i][j] = dom.Find(tableId).Find("tr").Find("td").Eq(i*(tdNum/trNum)+j).Text() 73 } 74 } 75 76 return tableCache 77 }
输出结果如下:
标签:string,tr,html,Golang,goquery,HTML,table,Table,Find 来源: https://www.cnblogs.com/merrynuts/p/13130204.html