方法一:webbrowser的outhtml法

利用webbrowser打開網頁,然後通過outhtml輸出源碼,再加以分析利用。

方法二:WebClient的Downloaddata法

利用webclient下載源碼,再加以分析利用。

方法三:XMLHTTP及流對象法

利用MSXML2.XMLHTTP與adodb.stream獲取源碼,再加以分析利用。

經過多次的測試,獲得源碼速度的排序是:

方法三 > 方法一 > 方法二

也就是說,用XMLHTTP的方法是獲得網頁源碼最快的方式。方法二所用時間是方法三的三倍,是方法一的二倍

下面我把幾個方法的主要過程(函數)列出來,大家可以參考指正

XMLHTTP及流對象法的基本函數

Function GetPage(ByVal url)

        Dim Retrieval
        Retrieval = CreateObject("MSXML2.XMLHTTP")
        With Retrieval
            .Open("Get", url, False) ', "", ""
              .Send()
            GetPage = BytesToBstr(.ResponseBody)
        End With

        Retrieval = Nothing
End Function
Function BytesToBstr(ByVal body)
        Dim objstream
        objstream = CreateObject("adodb.stream")
        objstream.Type = 1
        objstream.Mode = 3
        objstream.Open()
        objstream.Write(body)
        objstream.Position = 0
        objstream.Type = 2
        objstream.Charset = "utf-8"       '這裡是設定編碼的
        BytesToBstr = objstream.ReadText
        objstream.Close()
        objstream = Nothing
    End Function

WebClient的Downloaddata法的基本函數

Function gethtml(ByVal u As String, ByVal bm As String) As String
        Dim Doc As New WebClient
        Dim T As String = ""
        Try
            If bm = "utf-8" Then
                T = System.Text.Encoding.UTF8.GetString(Doc.DownloadData(u))
            ElseIf bm = "gb2312" Then
                T = System.Text.Encoding.Default.GetString(Doc.DownloadData(u))
            End If
            Return T
        Catch ex As Exception
            Return ""
        End Try
    End Function

webbrowser的outhtml法的基本函數

利用WebBrowser的DocumentCompleted,獲取webBrowser1.Document.Body.OuterHtml





=================================
VB.net保存遠程文件到本地並用進度條進行顯示的方法
 

首先在form裡添加一個ProgressBar,命名為prg1

然後指定要下載的文件路徑比如

dim remote as string ="http://www.abc.com/aa.exe" '遠程文件

dim local as string ="bb.exe" '本地文件

然後進行調用:

DownloadFile(remote, local,Me.prg1)

主函數:

Public Shared Sub DownloadFile(ByVal URL As String, ByVal Filename As String, ByVal Prog As ProgressBar)
    Dim Myrq As HttpWebRequest = HttpWebRequest.Create(URL)
    Dim myrp As HttpWebResponse = Myrq.GetResponse
    Dim totalBytes As Long = myrp.ContentLength
    Prog.Maximum = totalBytes
    Dim st As Stream = myrp.GetResponseStream
    Dim so As Stream = New FileStream(Filename, FileMode.Create)
    Dim totalDownloadedByte As Long = 0
    Dim by(1024) As Byte
    Dim osize As Integer = st.Read(by, 0, by.Length)
    While osize > 0
        totalDownloadedByte = osize + totalDownloadedByte
        System.Windows.Forms.Application.DoEvents()
        so.Write(by, 0, osize)
        Prog.Value = totalDownloadedByte
        osize = st.Read(by, 0, by.LongLength)
    End While
    so.Close()
    st.Close()
End Sub

arrow
arrow

    dark99 發表在 痞客邦 留言(0) 人氣()