『壹』 如何使用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