Private Sub Command1_Click()
Dim htmlDoc As Object
Set htmlDoc = CreateObject("HTMLFILE")
htmlDoc.designMode = "on"
htmlDoc.Write GetData("http://tieba.baidu.com/", "GBK")
'将HTML代码写入到"HTMLFILE"对像里,此时的htmlDoc可以像操作Webbrowser一样操作
Text1.Text = htmlDoc.body.innerText
'过滤HTML标签并提取网页文字
End Sub
Public Function GetData(ByVal Url As String, ByVal CodeBase As String) As Variant
Dim XMLHTTP As Object
Set XMLHTTP = CreateObject("Microsoft.XMLHTTP")
XMLHTTP.Open "Get", Url, True
XMLHTTP.send
'--------------------------------------发送数据
While XMLHTTP.ReadyState <> 4
DoEvents
Wend
'--------------------------------------函数返回
GetData = XMLHTTP.ResponseBody
If CStr(GetData) <> "" Then GetData = BytesToBstr(GetData, CodeBase)
Set XMLHTTP = Nothing
End Function
Public Function BytesToBstr(strBody, CodeBase) '编码转换("UTF-8"或者"GB2312"或者"GBK")
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