继续获得更多的网页信息:
获得网页上所有图片信息:
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 教程 目录