加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

vb.net 教程 12-3 HtmlElement类 2

发布时间:2020-12-17 08:13:59 所属栏目:百科 来源:网络整理
导读:在上上一节中,学习了如何获得网页上的所有图片,实际上是获得的所有的图片相关的HtmlElement。获得的信息类似于: 图片1: IMG border=0 alt=QQREC软件 src="img/logo.png" width=194 height=50 图片2: IMG style="VERTICAL-ALIGN: bottom" border=0 src="im

在上上一节中,学习了如何获得网页上的所有图片,实际上是获得的所有的图片相关的HtmlElement。获得的信息类似于:

图片1: <IMG border=0 alt=QQREC软件 src="img/logo.png" width=194 height=50>
图片2: <IMG style="VERTICAL-ALIGN: bottom" border=0 src="img/png-1186.png" width=25 height=25>

……

那么,如果我们仅仅是希望获得图片的网址呢?

当然,可以使用String的方法来拆分获得的字符串,比如以 src=" 开头到下一个 " 的这一段数据,

看起来还是比较复杂的,幸运的是,HtmlElement类提供了GetAttribute()方法来获得该元素某个属性的值:

例如:

获得元素的宽度值:.GetAttribute("width")

获得元素的图片地址:.GetAttribute("SRC")

修改后的图片信息代码如下:

    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.GetAttribute("SRC") & ControlChars.CrLf
            'htmlInfo &= "图片" & imgCount & ": " & img.OuterHtml & ControlChars.CrLf
        Next
        txtInfo.Text = htmlInfo
    End Sub

运行效果:


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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读