VB获取网页源代码的五种方法
添加microsoft ineternet transfor conctrol6.0 控件
方法2:XMLHTTP
'如果出现乱码,UTF-8可改为GB2312 Public Function GetBody(ByVal URL$, Optional ByVal Coding$ = "GB2312") Dim ObjXML On Error Resume Next Set ObjXML = CreateObject("Microsoft.XMLHTTP") With ObjXML .Open "Get", URL, False, "", "" .setRequestHeader "If-Modified-Since", "0" .Send GetBody = .ResponseBody End With GetBody = BytesToBstr(GetBody, Coding) Set ObjXML = Nothing End Function Public Function BytesToBstr(strBody, CodeBase) Dim ObjStream Set ObjStream = CreateObject("Adodb.Stream") With ObjStream .Type = 1 .Mode = 3 .Open .Write strBody .Position = 0 .Type = 2 .Charset = CodeBase BytesToBstr = .ReadText .Close End With Set ObjStream = Nothing End Function Private Sub Command1_Click() u = "http://wenku.baidu.com/search?word=vb&lm=0&od=0&fr=top_search" Text1.Text = GetBody(u) '最好用richbox富文本框 End Sub
方法3:WinHttp
Private Sub Command4_Click()
Set IEread = CreateObject("WinHttp.WinHttpRequest.5.1")
CallByName IEread, "Open", VbMethod, "GET", URLaddr, True
CallByName IEread, "Send", VbMethod
CallByName IEread, "WaitForResponse", VbMethod
aa = CallByName(IEread, "ResponseText", VbMethod)
RichTextBox1.Text = aa
End Sub
方法4:'利用WebBrowser控件
Private Sub Command1_Click()
Dim strContent As String, i As Integer
Text1 = ""
For i = 0 To WebBrowser1.Document.All.length - 1
If WebBrowser1.Document.All(i).tagName = "HTML" Then
strContent = strContent & WebBrowser1.Document.All(i).innerHTML
Exit For
End If
Next i
Text1 = strContent
End Sub
方法5:利用Inet控件的Execute方法
Private Sub Command5_Click()
Text1 = ""
Inet1.Execute Text2.Text, "GET"
While Inet1.StillExecuting
DoEvents
Wend
Text1.Text = Inet1.GetChunk(Len(Text1.Text))
End Sub