将XMLHTTP组件写成类方便获取网页代码

'//VBS利用XMLHTTP组件写成类 @CODE BY Broly
'//声明:代码仅作学习研究之用。使用前请三思而行,产生不良后果均与本人无关!
Class GetPage
  Private mstrUrl,xmlHttp,objstream
  PublicPropertyLet Url(strUrl)
    mstrUrl=strUrl
  EndProperty
  PublicFunction GetUTFPage()
    If mstrUrl=""ThenExitFunction
    Set xmlHttp=CreateObject("MSXML2.XMLHTTP")
    xmlHttp.open "GET",mstrUrl,false
    xmlHttp.send()
    If xmlHttp.readystate<>4then
    ExitFunction
    EndIf
    GetUTFPage=xmlHttp.responseText
    Set xmlHttp=Nothing
    If err.number<>0then err.Clear
  EndFunction
  PublicFunction GetGBKPage()
    If mstrUrl=""ThenExitFunction
    Set xmlHttp=CreateObject("MSXML2.XMLHTTP")
    xmlHttp.open "GET",mstrUrl,false
    xmlHttp.send()
    If xmlHttp.readystate<>4then
    ExitFunction
    EndIf
    GetGBKPage=BytesToBSTR(xmlHttp.responseBody,"GB2312")
    Set xmlHttp=nothing
    If err.number<>0then err.Clear
    EndFunction
   PrivateFunction BytesToBstr(body,Cset)
    Set objstream =CreateObject("adodb.stream")
    With objstream
      .Type = 1
      .Mode = 3
      .Open
      .Write body
      .Position = 0
      .Type = 2
      .Charset = Cset
      BytesToBstr = .ReadText
      .Close
    Endwith
    Set objstream = nothing
  EndFunction

EndClass

使用方法:

1.编写VBScript代码时,添加以上表格。(建议放在代码开头)

2.此类包含一个Url属性,两个方法GetGBKPage和GetUTFPage

  现在主流的网页编码有GBK和UTF

     方法 GetGBKPage 获取网页编码为GBK的网页代码

     方法 GetGBKPage 获取网页编码为GBK的网页代码

3.通过次类可以方便获取网页代码。


注:暂时支持这个简单的功能,更多的以后有时间会完善。

                                                            —— Broly

                                                               2011.1.24


例:

Class GetPage
  Private mstrUrl,xmlHttp,objstream
  PublicPropertyLet Url(strUrl)
    mstrUrl=strUrl
  EndProperty
  PublicFunction GetUTFPage()
    If mstrUrl=""ThenExitFunction
    Set xmlHttp=CreateObject("MSXML2.XMLHTTP")
    xmlHttp.open "GET",mstrUrl,false
    xmlHttp.send()
    If xmlHttp.readystate<>4then
    ExitFunction
    EndIf
    GetUTFPage=xmlHttp.responseText
    Set xmlHttp=Nothing
    If err.number<>0then err.Clear
  EndFunction
  PublicFunction GetGBKPage()
    If mstrUrl=""ThenExitFunction
    Set xmlHttp=CreateObject("MSXML2.XMLHTTP")
    xmlHttp.open "GET",mstrUrl,false
    xmlHttp.send()
    If xmlHttp.readystate<>4then
    ExitFunction
    EndIf
    GetGBKPage=BytesToBSTR(xmlHttp.responseBody,"GB2312")
    Set xmlHttp=nothing
    If err.number<>0then err.Clear
    EndFunction
   PrivateFunction BytesToBstr(body,Cset)
    Set objstream =CreateObject("adodb.stream")
    With objstream
      .Type = 1
      .Mode = 3
      .Open
      .Write body
      .Position = 0
      .Type = 2
      .Charset = Cset
      BytesToBstr = .ReadText
      .Close
    Endwith
    Set objstream = nothing
  EndFunction
EndClass
Dim url,objPage,text
url="http://www.gougou.com/"
Set objPage=New GetPage
objPage.Url=url
text=objPage.GetGBKPage()
MsgBox text

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值