locked
FAQs: 在VB.NET中如何保存网页? RRS feed

答案

  • 此处提供几种方法:

     

    1) 你可以使用System.Net.WebClient类直接将网页保存成html文件。

     

    Public Class Form1

        Private Sub Button1_Click(ByVal sender As System.Object, _

                            ByVal e As System.EventArgs) Handles Button1.Click

            Dim myWebClient As New System.Net.WebClient

            myWebClient.DownloadFile("http://forums.microsoft.com/MSDN/default.aspx", _

                            "C:\msdn.html")

        End Sub

    End Class

     

     

    2) 你可以使用CDOADODB组件将完整的网页保存成.mht文件。

    首先添加两个引用:“Microsoft CDO For Windows 2000 Library”“Microsoft ActiveX Data Objects 2.5 Library”

    项目菜单 -> 添加引用... -> COM标签

     

    Imports ADODB

    Imports CDO

    Public Class Form1

     

        Private Sub Button1_Click(ByVal sender As System.Object, _

                            ByVal e As System.EventArgs) Handles Button1.Click

            SavePage("http://forums.microsoft.com/MSDN/default.aspx", "C:\msdn.mht")

        End Sub

     

        Private Sub SavePage(ByVal Url As String, ByVal FilePath As String)

            Dim iMessage As CDO.Message = New CDO.Message

            iMessage.CreateMHTMLBody(Url, _

            CDO.CdoMHTMLFlags.cdoSuppressNone, "", "")

            Dim adodbstream As ADODB.Stream = New ADODB.Stream

            adodbstream.Type = ADODB.StreamTypeEnum.adTypeText

            adodbstream.Charset = "US-ASCII"

            adodbstream.Open()

            iMessage.DataSource.SaveToObject(adodbstream, "_Stream")

            adodbstream.SaveToFile(FilePath, _

                      ADODB.SaveOptionsEnum.adSaveCreateOverWrite)

        End Sub

     

    End Class

     

     

       相关帖子:

    http://social.msdn.microsoft.com/forums/en-US/Vsexpressvb/thread/c09c132c-5046-42f1-b1f8-11a5d16f458c/

     

    3) 你可以通过WebBrowser控件保存网页内容。

     

    Public Class Form1

     

        Private Sub Form1_Load(ByVal sender As System.Object, _

                            ByVal e As System.EventArgs) Handles MyBase.Load

            WebBrowser1.Navigate("http://forums.microsoft.com/MSDN/default.aspx")

        End Sub

     

        Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, _

                            ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) _

                            Handles WebBrowser1.DocumentCompleted

            '获取入口html 代码并保存成.html 文件

            Dim MSDNpage As String = WebBrowser1.DocumentText

            My.Computer.FileSystem.WriteAllText("C:\msdn1.html", MSDNpage, True)

            ' 获取页面主体内容文本并保存成.txt文件

            Dim MSDNpage As String = WebBrowser1.Document.Body.InnerText

            My.Computer.FileSystem.WriteAllText("C:\msdn2.txt", MSDNpage, True)

        End Sub

     

    End Class

     

     


    如果您对我们的论坛在线支持服务有任何的意见或建议,请通过邮件告诉我们。
    MSDN 论坛好帮手 立刻免费下载  MSDN 论坛好帮手
    2011年3月2日 12:13