'1、輸入url目標網頁地址,返回值getHTTPPage是目標網頁的html代碼 function getHTTPPage(url) dim Http set Http=server.createobject("MSXML2.XMLHTTP" Http.open "GET",url,false Http.send() if Http.readystate>4 then exit function end if getHTTPPage=bytesToBSTR(Http.responseBody,"GB2312" set http=nothing if err.number>0 then err.Clear end function
'2、轉換亂瑪,直接用xmlhttp調用有中文字符的網頁得到的將是亂瑪,可以通過adodb.stream組件進行轉換 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
'下面試著調用http://wmjie.51.net/swords的html內容 Dim Url,Html Url="http://wmjie.51.net/swords/" Html = getHTTPPage(Url) Response.write Html %>
' Add a header to give it a file name: Response.AddHeader "Content-Disposition", _ "attachment;filename=mitchell-pres.zip"
' Specify the content type to tell the browser what to do: Response.ContentType = "application/zip"
' Binarywrite the bytes to the browser Response.BinaryWrite xml.responseBody
Set xml = Nothing %>
------------------------------------- 如何寫ASP入庫小偷程序 入庫小偷的原理也很簡單:就是用XMLHTTP遠程讀取網頁的內容,然后根據需要,對讀到的內容進行加工(過濾,替換,分類),最后得到自己需要的數據,加入到數據庫中。 首先:我們先用XMLHTTP讀取遠程網頁(我的另一片文章中有介紹)。 其次:對內容進行過濾,這個是比較關鍵的步驟,比如說,我要從遠程網頁上提取出所有url連接,我應該怎么做呢? 代碼: ‘這里用的是正則式 Set objRegExp = New Regexp '建立對象 objRegExp.IgnoreCase = True '大小寫忽略 objRegExp.Global = True '全局為真 objRegExp.Pattern = "http://.+?" '匹配字段 set mm=objRegExp.Execute(str) '執行查找,str為輸入參數 For Each Match in mm '進入循環 Response.write(Match.Value) '輸出url地址 next
然后,我們需要根據需要做一些替換功能,把不必要的數據替換掉,這個比較簡單,用Replace函數即可。 最后,進行數據庫操作 ------------------------------- 一個例子 代碼: % On Error Resume Next Server.ScriptTimeOut=9999999 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
'然后調用XMLHTTP組件創建一個對象并進行初始化設置。
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
'處理抓取回來的數據需要調用adodb.stream組件并進行初始化設置。%>
'以下即為頁面顯示部分
% Dim wstr,str,url,start,over,city '定義一些需要使用到的變量
city = Request.QueryString("id") '程序傳回的ID變量(即用戶選擇的城市)賦給id
body = replace(body,"skin1","天氣預報 - 斯克網絡") body = replace(body,"http://appnews.qq.com/cgi-bin/news_qq_search?city","tianqi.asp?id")
'本程序中已經完成了替換的工作,如果有其他需要的話可以繼續進行類似的替換操作。
response.write body 引用: 遠程獲取內容,并將內容存在本地電腦上,包括任何文件
% '----------遠程獲取內容,并將內容存在本地電腦上,包括任何文件!---------- 'On Error Resume Next 'Set the content type to the specific type that you are sending. 'Response.ContentType = "IMAGE/JPEG" '-------------------------------定義輸出格式-----------------------------
Path=request.querystring("p") sPath = Path if left(lcase(path),7) > "http://"; then '-------------如果前面沒有http就是本地文件,交給LocalFile處理------------ LocalFile(path) else '--------------------否則為遠程文件,交給RemoteFile處理------------------ RemoteFile(Path) end if 'Response.Write err.Description
sub LocalFile(Path) '-------------------如果為本地文件則簡單的跳轉到該頁面------------------- Response.Redirect Path End Sub
Sub RemoteFile(sPath) '-------------------------處理遠程文件函數------------------------------ FileName = GetFileName(sPath) '-------------GetFileName為把地址轉換為合格的文件名過程------------- FileName = Server.MapPath("/UploadFile/Cache/" FileName) Set objFso = Server.CreateObject("Scripting.FileSystemObject") 'Response.Write fileName if objFso.FileExists(FileName) Then '--------------檢查文件是否是已經訪問過,如是,則簡單跳轉------------ Response.Redirect "/uploadfile/cache/" GetFileName(path) Else '----------------否則的話就先用GetBody函數讀取---------------------- 'Response.Write Path t = GetBody(Path) '-----------------用二進制方法寫到瀏覽器上-------------------------- Response.BinaryWrite t Response.Flush '-----------------輸出緩沖------------------------------------------ SaveFile t,GetFileName(path) '------------------將文件內容緩存到本地路徑,以待下次訪問----------- End if Set objFso = Nothing End Sub
Function GetBody(url) '-----------------------本函數為遠程獲取內容的函數--------------------- 'on error resume next 'Response.Write url Set Retrieval = CreateObject("Microsoft.XMLHTTP") '----------------------建立XMLHTTP對象----------------------------- With Retrieval .Open "Get", url, False, "", "" '------------------用Get,異步的方法發送----------------------- .Send 'GetBody = .ResponseText GetBody = .ResponseBody '------------------函數返回獲取的內容-------------------------- End With Set Retrieval = Nothing 'response.Write err.Description End Function
Function GetFileName(str) '-------------------------本函數為合格化的文件名函數------------------- str = Replace(lcase(str),"http://";,"") str = Replace(lcase(str),"http://","/") str = Replace(str,"/","") str = replace(str,vbcrlf,"") GetFileName = str End Function
sub SaveFile(str,fName) '-------------------------本函數為將流內容存盤的函數------------------- 'on error resume next Set objStream = Server.CreateObject("ADODB.Stream") '--------------建立ADODB.Stream對象,必須要ADO 2.5以上版本--------- objStream.Type = adTypeBinary '-------------以二進制模式打開------------------------------------- objStream.Open objstream.write str '--------------------將字符串內容寫入緩沖-------------------------- 'response.Write fname objstream.SaveToFile "c:\inetpub\myweb\uploadfile\cache\" fName,adSaveCreateOverWrite '--------------------將緩沖的內容寫入文件-------------------------- 'response.BinaryWrite objstream.Read objstream.Close() set objstream = nothing '-----------------------關閉對象,釋放資源------------------------- 'response.Write err.Description End sub %>