版权声明:本文为博主原创文章,转载请在显著位置标明本文出处以及作者网名,未经作者允许不得用于商业目的。
本节谈谈获得网页上表格的信息
直接上代码:
Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click
Dim doc As mshtml.HTMLDocument
doc = wbMain.Document.DomDocument
Dim tab As mshtml.HTMLTable
tab = doc.getElementsByTagName("table").item(0)
Dim myrow As mshtml.HTMLTableRow
myrow = tab.rows.item(0)
Dim mycell As mshtml.HTMLTableCell
mycell = myrow.cells.item(0)
txtInfo.Text = "表格中第一行第一个数据:" & mycell.innerText
End Sub
看看运行结果:
为了更清晰地分析,将上述代码修改为:
Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click
Dim doc As mshtml.HTMLDocument
doc = wbMain.Document.DomDocument
Dim tab As mshtml.HTMLTable
tab = doc.getElementsByTagName("table").item(0)
Dim myrow As mshtml.HTMLTableRow
myrow = tab.rows.item(0)
Dim mycell As mshtml.HTMLTableCell
mycell = myrow.cells.item(0)
'txtInfo.Text = "表格中第一行第一个数据:" & mycell.innerText
txtInfo.Text = "表格中第一行第一个数据:" & mycell.innerHTML
End Sub
运行结果:
这下看出来了。
tab = doc.getElementsByTagName("table").item(0)
获得网页第一个table,也就是最外面的table,然后获得的是这个table的第一个格子里面的内容,虽然内容也是一个table。
所以想要获得具体哪个表格内的内容,还得老老实实地分析网页代码。
表格内容也可以修改的,不过好像没什么用:
Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click
Dim doc As mshtml.HTMLDocument
doc = wbMain.Document.DomDocument
Dim tab As mshtml.HTMLTable
tab = doc.getElementsByTagName("table").item(0)
Dim myrow As mshtml.HTMLTableRow
myrow = tab.rows.item(0)
Dim mycell As mshtml.HTMLTableCell
mycell = myrow.cells.item(0)
'txtInfo.Text = "表格中第一行第一个数据:" & mycell.innerText
txtInfo.Text = "表格中第一行第一个数据:" & mycell.innerHTML
mycell.innerText = "abcdefg"
End Sub
运行结果:
由于.net平台下C#和vb.NET很相似,本文也可以为C#爱好者提供参考。
————————————————
版权声明:本文为CSDN博主「VB.Net」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。