㈠ 用VB.NET載入網頁源碼中的字元
'首先用webclient獲得貼吧首頁的源碼。這里以vb,net貼吧為例
Private Sub BtnGetNum_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnGetNum.Click
Dim url As String = "http://tieba..com/f?kw=vb.net"'vb.net吧的首頁
Dim MyClient As System.Net.WebClient = New System.Net.WebClient
Dim MyReader As New System.IO.StreamReader(MyClient.OpenRead(url), System.Text.Encoding.Default) '定義新的文件流並讀取網頁文件數據
Dim longTxt As String = MyReader.ReadToEnd '寫入
'分析源碼,獲取主題數
Dim startStr As String = "共有主題數"
Dim startNum As Long = InStr(longTxt, startStr) + Len(startStr) + 18
Dim resStr As String = ""
Do Until Mid(longTxt, startNum, 1) = 「<"
resStr = resStr & Mid(longTxt, startNum, 1)
startNum = startNum + 1
Loop
Label1.Text = resStr '顯示主題數
End Sub
㈡ 如何用vb.net獲得網頁的源代碼
Dim url As String=" 網址"
Dim httpReq As System.Net.HttpWebRequest
Dim httpResp As System.Net.HttpWebResponse
Dim httpURL As New System.Uri(url)
httpReq = CType(WebRequest.Create(httpURL), HttpWebRequest)
httpReq.Method = "GET"
httpResp = CType(httpReq.GetResponse(), HttpWebResponse)
httpReq.KeepAlive = False ' 獲取或設置一個值,該值指示是否與
Internet資源建立持久連接。
Dim reader As StreamReader = _
New StreamReader(httpResp.GetResponseStream,
System.Text.Encoding.GetEncoding(-0))
Dim respHTML As String = reader.ReadToEnd() 'respHTML就是網頁源代碼