u3u3博客

u3u3的小天地

« VB常用函数整理Aspen Plus中查物性的两种方法 »

asp截取网页中部分字符的代码(获取title之间的网页标题)

asp截取网页中部分字符的代码。以截取title之间的字符为例子。希望大家能够举一反三!有问题请留言转帖请标注:“来自www.u3u3.com.cn”字样

1、新建一个填URL的文件。我这里用“index.asp”(你可以根据需要改成其他)。其全部代码如下:

<HTML>

<HEAD>

<title>Get URL Text</title>

</HEAD>

<BODY>

<!-- Author: www.u3u3.com.cn -->

<form action="URLGetMSINet.asp">

<table border=0>

<tr><td>URL</td><td><input type=text name=txtURL value=""></td></tr>

</table>

<input type=submit value="Get Text">

</form>

</BODY>

</HTML>

2、再新建一个asp文件。文件名称为:“URLGetMSInet.asp”。此文件名要与上页代码中<form action="URLGetMSINet.asp"> 的引号内的文件名保持一致!!此文件全代码如下:

<%@ Language=VBScript %>

<HTML>

<HEAD>

<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">

<title>Get URL Text</title>

</HEAD>

<BODY>

<!-- Author: www.u3u3.com.cn -->

<%

'Set obj = CreateObject("InetCtls.Inet")

Set obj = Server.CreateObject("InetCtls.Inet.1")

obj.RequestTimeOut=20

sRequest = trim(Request("txtURL"))

sText = obj.OpenURL (CStr(sRequest))

sText = "<pre>" & vbcrlf & Server.HTMLEncode(sText) & vbCRLF & "</pre>"

'======='下标越界: '[number: 1]'=数据为空!

'使用split()函数不能有“<”或“>”字符

dim filearray,filearray2

start_u3=cstr("&lt;title&gt;")

end_u3=cstr("&lt;/title&gt;")

filearray=split(sText,start_u3)

if filearray(1)=empty then

sText="Sorry! Server Error! u3u3"

else

filearray2=split(filearray(1),end_u3)

sText=filearray2(0)

end if

response.Write(sText)

set obj = nothing

%>

</BODY>

</HTML>

3、测试代码:把这俩文件放到你的web站上。然后在IE中输入:“http://你的web站的URL/index.asp”。回车

出现在URL后的框内输入一个网站地址。如:“http://www.baidu.com”记得要有“http;//”头。

回车后,出现“百度一下,你就知道 ”。这就是说你成功截取了百度网站首页的<title></title>之间网站标题!

希望大家能够举一反三!有问题请留言转帖请标注:“来自www.u3u3.com.cn”字样。

  • 相关文章:
  • quote 1.www.kuozhi.com
  • 按你的设计,出现以下错误,如何处理!!!!!急!!!!!!急!!!!!!急!!!!!!急!!!!!!急!!!!!!急!!!!!!急!!!!!!

    Server 对象 错误 'ASP 0177 : 800401f3'

    Server.CreateObject 失败

    /zz_admin_GetURLMSInet.asp,行 23

    800401f3
    u3u3 于 2009-1-7 21:25:45 回复
    你是不是对代码有所改动?
    还是你的服务器有问题?代码没错的.我测试完了才发布上来的.
    你把zz_admin_GetURLMSInet.asp,行 23 的代码发上来我看看.或者留下QQ号,把你改完的代码发给我.
    我估计你肯定改过代码,因此出错了.
    比如start_u3=cstr("&lt;title&gt;")这里的引号里的字符你知道什么意思吗?
  • 2009-1-7 21:25:45 回复该留言
  • quote 2.www.kuozhi.com
  • 我现在改为
    <%
    '''''''截取网站标题要用到的函数
    On Error Resume Next
    Server.ScriptTimeOut=20
    Function getHTTPPage(Path)
    t = GetBody(Path)
    getHTTPPage=BytesToBstr(t,"GB2312")
    End function

    Function GetBody(url)
    on error resume next
    Set Retrieval = CreateObject("Microsoft.XMLHTTP")
    With Retrieval
    .Open "Get", url, False, "", ""
    .Send
    GetBody = .ResponseBody
    End With
    Set Retrieval = Nothing
    End Function

    Function BytesToBstr(body,Cset)
    dim objstream
    set objstream = Server.CreateObject("adodb.stream")
    objstream.Type = 1
    objstream.Mode =3
    objstream.Open
    objstream.Write body
    objstream.Position = 0
    objstream.Type = 2
    objstream.Charset = Cset
    BytesToBstr = objstream.ReadText
    objstream.Close
    set objstream = nothing
    End Function
    Function Newstring(wstr,strng)
    Newstring=Instr(lcase(wstr),lcase(strng))
    if Newstring<=0 then Newstring=Len(wstr)
    End Function
    %>
    希望多交流,zynamecom@yahoo.com.cn网址www.kuozhi.com
  • 2009-1-8 22:24:03 回复该留言
  • quote 3.碧海蓝太难
  • http://www.zyhoo.org
  • 我按你的来了也出错,不知道什么原因。能帮忙整个学习下吗?
    u3u3 于 2009-6-10 20:38:22 回复
    把错误提示发上来。我的代码经过测试的。如果没有更改是不会出错的。
    思源 于 2009-6-12 13:38:56 回复
    错误类型:
    Server 对象, ASP 0177 (0x800401F3)
    无效的类别字符串
    /URLGetMSINet.asp, 第 21 行

    这是错误提示

    Set obj = Server.CreateObject("InetCtls.Inet.1")
  • 2009-6-12 13:38:56 回复该留言

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

日历

最新评论及回复

最近发表

Powered By u3u3

Copyright 2007-2015 All Rights Reserved.  
京ICP备06008234