在站长工具中,经常看到判断一个页面的关键词密度,他是怎么实现的获取外部网页内容的呢。是通过js的ajax,获取后再计算。
下面是获取外部网页代码的代码。也可以查看效果:javascript读取网页代码
下面是详细代码
<script language=javascript>
function check(url)
{
var xmlHttp = new ActiveXObject('Microsoft.XMLHTTP');
var chkUrl = url;
xmlHttp.open('get', chkUrl, false);
try{
xmlHttp.send();
}
catch(e){
window.alert('出现异常');
}
oSource.value=xmlHttp.responseText;
// alert(bytes2BSTR(xmlHttp.responseText));
fn.document.body.innerHTML="<base href='"+url+"'>"+xmlHttp.responseText;
}
</script>
<Script Language=Vbscript>
Function getHTTPPage(url)
on error resume next
dim http,code
set http= createobject("Microsoft.XMLHTTP")
Http.open "GET",url,false
Http.send()
if Http.readystate<>4 then
exit function
end if
code=bytes2BSTR(Http.responseBody)
oSource.value=code
fn.document.body.innerHTML="<base href='"+url+"'>"+code
set http=nothing
if err.number<>0 then err.Clear
End function
Function bytes2BSTR(vIn)
strReturn = ""
For i = 1 To LenB(vIn)
ThisCharCode = AscB(MidB(vIn,i,1))
If ThisCharCode < &H80 Then
strReturn = strReturn & Chr(ThisCharCode)
Else
NextCharCode = AscB(MidB(vIn,i+1,1))
strReturn = strReturn & Chr(CLng(ThisCharCode) * &H100 + CInt(NextCharCode))
i = i + 1
End If
Next
bytes2BSTR = strReturn
End Function
</script>
<body>
<div align="center">输入域名点击打开即可获取网站内容: <br>
<textarea cols="100" rows="10" id="oSource"></textarea>
<br>
<input name=url value="//www.reelogic.com">
<input type=button value="打开" onclick=getHTTPPage(url.value)><br>
</div>
更多信息请查看IT技术专栏