㈠ 用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就是网页源代码