『壹』 如何使用vb.net來解析網頁源代碼,最終獲取所需數據
使用webbrowser控制項來載入網頁,然後再 Private Sub WebBrowser1_DocumentCompleted下通過使用WebBrowser1.Document.Body.InnerHtml 來獲取網頁的源代碼,或使用 WebBrowser1.Document.Body.InnerText來獲取網頁中的文本。之後可以通過字元串控制指令或者正則表達式來精確獲取到你所需的數據。
『貳』 VB.NET 不使用控制項獲取某網頁源代碼
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim MyClient As Net.WebClient = New Net.WebClient
Dim MyReader As New System.IO.StreamReader(MyClient.OpenRead("http://wap..com"), System.Text.Encoding.Default)
Dim MyWebCode As String = MyReader.ReadToEnd
Me.RichTextBox1.Text = MyWebCode
MyReader.Close()
End Sub
『叄』 VB.NET如何使用正則表達式讀取網頁源碼中的指定內容
<divclass="cont">[sS]*?href=["']?([^"']*)["']?[sS]*?title=["']?([^"]*)["']?
『肆』 如何用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就是網頁源代碼
『伍』 vb.NET webbrowser 修改網頁源碼
Dim c
Private Sub Form_Load()
URL = "http://..com/question/123100932.html"
c = 0
WebBrowser1.Navigate URL
End Sub
Private Sub WebBrowser1_DocumentComplete(ByVal pDisp As Object, URL As Variant)
If c = 1 Then Exit Sub '這個是為了防止這個過程無限執行下去
c = c + 1
tmp = WebBrowser1.Document.body.innerhtml
tmp = Replace(tmp, "88753967", "好玩嗎?")
WebBrowser1.Document.body.innerhtml = tmp
End Sub