vb.net 教程 12-2 HtmlDocument类 3

版权声明:本文为博主原创文章,转载请在显著位置标明本文出处以及作者网名,未经作者允许不得用于商业目的。
继续获得更多的网页信息:

获得网页上所有图片信息:

    Private Sub btnImages_Click(sender As Object, e As EventArgs) Handles btnImages.Click
        Dim htmlInfo As String = ""
        Dim imgCount As Integer = 0
        Dim imgs As HtmlElementCollection
        imgs = wbMain.Document.Images
        For Each img As HtmlElement In imgs
            imgCount += 1
            htmlInfo &= "图片" & imgCount & ": " & img.OuterHtml & ControlChars.CrLf
        Next
        txtInfo.Text = htmlInfo
    End Sub

运行时如图:

获得网页上所有链接信息:

    Private Sub btnLinks_Click(sender As Object, e As EventArgs) Handles btnLinks.Click
        Dim htmlInfo As String = ""
        Dim linkCount As Integer = 0
        Dim lnks As HtmlElementCollection
        lnks = wbMain.Document.Links
        For Each lnk As HtmlElement In lnks
            linkCount += 1
            htmlInfo &= "链接" & linkCount & ": " & lnk.OuterHtml & ControlChars.CrLf
        Next
        txtInfo.Text = htmlInfo
    End Sub

 

还可以通过All属性获得网页上的所有元素:

    Private Sub btnAllElement_Click(sender As Object, e As EventArgs) Handles btnAllElement.Click
        Dim htmlInfo As String = ""
        Dim htmlAlls As HtmlElementCollection
        htmlAlls = wbMain.Document.All
        For Each htmlSingle As HtmlElement In htmlAlls
            htmlInfo &= htmlSingle.TagName.ToUpper & ControlChars.CrLf
        Next
        txtInfo.Text = htmlInfo
    End Sub

 

以上代码用到了HtmlElement类,相关知识请参看下一节教程。

 

 

由于.net平台下C#和vb.NET很相似,本文也可以为C#爱好者提供参考。

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值