当前位置: 移动技术网 > IT编程>开发语言>Asp > XMLHttp ASP远程获取网页内容代码

XMLHttp ASP远程获取网页内容代码

2017年12月12日  | 移动技术网IT编程  | 我要评论

复制代码 代码如下:

url="http://www.csdn.net/"
wstr=gethttppage(url)
start=newstring(wstr,"资源精选<!-- 下载 -->")
over=newstring(wstr,"<div class=""friendlink"">")
body=mid(wstr,200,500)

response.write body

function gethttppage(url)
dim objxml
set objxml=createobject("msxml2.xmlhttp")'定义
objxml.open "get",url,false'打开
objxml.send()'发送
if objxml.readystate<>4 then '判断文档是否已经解析完,以做客户端接受返回消息
exit function
end if
gethttppage=bbytestobstr(objxml.responsebody)'返回信息,同时用函数定义编码
set objxml=nothing'关闭
if err.number<>0 then err.clear
end function

function newstring(wstr,strng)
newstring=instr(lcase(wstr),lcase(strng))
if newstring<=0 then newstring=len(wstr)
end function

function bbytestobstr(body)
dim objstream
set objstream = createobject("adodb.stream")
objstream.type = 1
objstream.mode =3
objstream.open
objstream.write body
objstream.position = 0
objstream.type = 2
objstream.charset = "gb2312"
'转换原来默认的utf-8编码转换成gb2312编码,否则直接用xmlhttp调用有中文字符的网页得到的将是乱码
bbytestobstr = objstream.readtext
objstream.close
set objstream = nothing
end function

function bytestobstr(body)
dim objstream
set objstream = createobject("adodb.stream")
objstream.type = 1
objstream.mode =3
objstream.open
objstream.write body
objstream.position = 0
objstream.type = 2
objstream.charset = "utf-8"
'转换原来默认的utf-8编码转换成gb2312编码,否则直接用xmlhttp调用有中文字符的网页得到的将是乱码
bytestobstr = objstream.readtext
objstream.close
set objstream = nothing
end function

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网