asp下利用xmlhttp获取网页内容的方法这个方法一般比较通用的,然后通过字符截取网页的内容。
代码如下:
url=“http://www.csdn.net/”
wstr=getHTTPPage(url)
start=Newstring(wstr,“资源精选
”)
over=Newstring(wstr,"<divclass="“friendlink”">")
body=mid(wstr,200,500)
response.writebody
FunctiongetHTTPPage(url)
dimobjXML
setobjXML=createobject(“MSXML2.XMLHTTP”)'定义
objXML.open"GET",url,false’打开
objXML.send()'发送
IfobjXML.readystate<>4then’判断文档是否已经解析完,以做客户端接受返回消息
exitfunction
EndIf
getHTTPPage=bBytesToBstr(objXML.responseBody)'返回信息,同时用函数定义编码
setobjXML=nothing’关闭
iferr.number<>0thenerr.Clear
EndFunction
FunctionNewstring(wstr,strng)
Newstring=Instr(lcase(wstr),lcase(strng))
ifNewstring<=0thenNewstring=Len(wstr)
EndFunction
FunctionbBytesToBstr(body)
dimobjstream
setobjstream=CreateObject(“adodb.stream”)
objstream.Type=1
objstream.Mode=3
objstream.Open
objstream.Writebody
objstream.Position=0
objstream.Type=2
objstream.Charset=“gb2312”
'转换原来默认的UTF-8编码转换成GB2312编码,否则直接用XMLHTTP调用有中文字符的网页得到的将是乱码
bBytesToBstr=objstream.ReadText
objstream.Close
setobjstream=nothing
endFunction
FunctionBytesToBstr(body)
dimobjstream
setobjstream=CreateObject(“adodb.stream”)
objstream.Type=1
objstream.Mode=3
objstream.Open
objstream.Writebody
objstream.Position=0
objstream.Type=2
objstream.Charset=“utf-8”
'转换原来默认的UTF-8编码转换成GB2312编码,否则直接用XMLHTTP调用有中文字符的网页得到的将是乱码域名交易
BytesToBstr=objstream.ReadText
objstream.Close
setobjstream=nothing
endFunction
在ASP下XMLHttp怎么利用远程获得网页代码?
最新推荐文章于 2021-06-04 12:21:57 发布