VB用CreateObject的方法获取指定网页源码

 1 Public Function GetBody(ByVal URL$, Optional ByVal Coding$ = "GB2312")  2 Dim ObjXML  3 On Error Resume Next
 4 Set ObjXML = CreateObject("Microsoft.XMLHTTP")  5 With ObjXML  6 .Open "Get", URL, False, "", ""
 7 .setRequestHeader "If-Modified-Since", "0"
 8 .Send  9 GetBody = .responseBody 10 End With
11 GetBody = BytesToBstr(GetBody, Coding) 12 Set ObjXML = Nothing
13 End Function
14 
15 Public Function BytesToBstr(strBody, CodeBase) 16 Dim ObjStream 17 Set ObjStream = CreateObject("Adodb.Stream") 18 With ObjStream 19 .Type = 1
20 .Mode = 3
21 .Open 22 .Write strBody 23 .Position = 0
24 .Type = 2
25 .Charset = CodeBase 26 BytesToBstr = .ReadText 27 .Close 28 End With
29 Set ObjStream = Nothing
30 End Function
31 '注意第一行代码中Coding$ = "GB2312" 表示获取的网页内容为GB2312编码格式,如果出现乱码,那么就换成Coding$ = "utf-8"
32 '返回值为: 33 a = GetBody("http://www.baidu.com")

本文转自:百度知道

你可能感兴趣的:(VB用CreateObject的方法获取指定网页源码)