vb.net 教程 12-4 msHtml 4

前端之家收集整理的这篇文章主要介绍了vb.net 教程 12-4 msHtml 4前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
本节谈谈获得网页上表格的信息
直接上代码
    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#爱好者提供参考。

学习更多vb.net知识,请参看 vb.net 教程 目录

原文链接:https://www.f2er.com/vb/256462.html

猜你在找的VB相关文章