ASP採集入庫生成本地文件的幾個函數

ASP網站數據採集程序製作:一個採集入庫生成本地文件的幾個FUCTION(可用來生成HTML靜態網頁)2008-09-01 08:57'

1:SaveFiles(byref from,byref tofile)
'作用 :利用流保存文件
' 參數 :from(遠程文件地址),tofile(保存文件位置)

'2:IsExists(byref filespec)
'作用 :利用fso檢測文件是否存在,存在返回true,不存在返回false
' 參數 :filespes(文件位置)

'3:IsFolder(byref Folder)
'作用 :利用fso檢測文件夾是否存在,存在返回true,不存在返回false
' 參數 :folder(文件夾位置)

'4:CreateFolder(byref fldr)
'作用 :利用fso創建文件夾
' 參數 :fldr(文件夾位置)

'5:SaveData(byref FromUrl,byref ToFiles)
'作用 :保存文件,並自動創建多級文件夾
' 參數 :fromurl(遠程文件地址),tofiles (保存位置)

'6:GetData(byref url,byref GetMode)
'作用 :取得遠程數據
' 參數 :url(遠程文件地址),getmode (模式:0爲二進制,1爲中文編碼)

'7:FormatImgPath(byref ImgUrl,byref ImgFolder,byref FristName,byref noimg)
'作用 :格式化遠程圖片地址爲本地位置
' 參數 :imgurl(遠程圖片地址),imgfolder (本地圖片目錄),fristname(加入的前綴名稱)

'有了以上這7個函數,你就可以做一個簡單的網站數據採集程序了,下面貼出實現的詳細代碼.
 

  1. '*****************************************************************  
  2. ' function  
  3. ' 作用 :利用流保存文件  
  4. ' 參數 :from(遠程文件地址),tofile(保存文件位置)  
  5. '*****************************************************************  
  6. Private Function SaveFiles(byref from,byref tofile)  
  7. Dim Datas  
  8. Datas=GetData(from,0)  
  9. Response.Write "保存成功:<font color=red>"&formatnumber(len(Datas)/1024*2,2)&"</font>Kb"  
  10. response.Flush  
  11. if formatnumber(len(Datas)/1024*2,2)>1 then  
  12. ADOS.Type = 1 
  13. ADOS.Mode =3 
  14. ADOS.Open  
  15. ADOS.write Datas  
  16. ADOS.SaveToFile server.mappath(tofile),2  
  17. ADOS.Close()  
  18. else  
  19. Response.Write "保存失敗:<font color=red>文件大小"&formatnumber(len(imgs)/1024*2,2)&"Kb,小於1K</font>"  
  20. response.Flush  
  21. end if  
  22. end function  
  23.  
  24. '*****************************************************************  
  25. ' function(私有)  
  26. ' 作用 :利用fso檢測文件是否存在,存在返回true,不存在返回false  
  27. ' 參數 :filespes(文件位置)  
  28. '*****************************************************************  
  29. Private Function IsExists(byref filespec)   
  30. If (FSO.FileExists(server.MapPath(filespec))) Then  
  31. IsExists = True 
  32. Else  
  33. IsExists = False 
  34. End If  
  35. End Function  
  36.  
  37.  
  38. '*****************************************************************  
  39. ' function(私有)  
  40. ' 作用 :利用fso檢測文件夾是否存在,存在返回true,不存在返回false  
  41. ' 參數 :folder(文件夾位置)  
  42. '*****************************************************************  
  43. Private Function IsFolder(byref Folder)  
  44. If FSO.FolderExists(server.MapPath(Folder)) Then   
  45. IsFolder = True 
  46. Else  
  47. IsFolder = False 
  48. End If  
  49. End Function  
  50.  
  51. '*****************************************************************  
  52. ' function(私有)  
  53. ' 作用 :利用fso創建文件夾  
  54. ' 參數 :fldr(文件夾位置)  
  55. '*****************************************************************  
  56. Private Function CreateFolder(byref fldr)   
  57. Dim f  
  58. Set f = FSO.CreateFolder(Server.MapPath(fldr))  
  59. CreateFolder = f.Path  
  60. Set f=nothing 
  61. End Function  
  62.  
  63. '*****************************************************************  
  64. ' function(公有)  
  65. ' 作用 :保存文件,並自動創建多級文件夾  
  66. ' 參數 :fromurl(遠程文件地址),tofiles (保存位置)  
  67. '*****************************************************************  
  68. Public Function SaveData(byref FromUrl,byref ToFiles)  
  69. ToFiles=trim(Replace(ToFiles,"//","/"))  
  70. flName=ToFiles 
  71. fldr="" 
  72. If IsExists(flName)=false then   
  73. GetNewsFold=split(flName,"/")  
  74. For i=0 to Ubound(GetNewsFold)-1  
  75. if fldr="" then  
  76.    fldr=GetNewsFold(i)  
  77. else  
  78.    fldrfldr=fldr&""&GetNewsFold(i)  
  79. end if  
  80. If IsFolder(fldr)=false then  
  81.    CreateFolder fldr  
  82. End if  
  83. Next  
  84. SaveFiles FromUrl,flName  
  85. End if  
  86. End function  
  87. '*****************************************************************  
  88. ' function(公有)  
  89. ' 作用 :取得遠程數據  
  90. ' 參數 :url(遠程文件地址),getmode (模式:0爲二進制,1爲中文編碼)  
  91. '*****************************************************************  
  92. Public Function GetData(byref url,byref GetMode)   
  93. 'on error resume next   
  94. SourceCode = OXML.open ("GET",url,false)  
  95. OXML.send()   
  96. if OXML.readystate<>4 then exit function  
  97. if GetMode=0 then  
  98. GetData = OXML.responseBody  
  99. else  
  100. GetData = BytesToBstr(OXML.responseBody)  
  101. end if  
  102. if err.number<>0 then err.Clear  
  103. End Function  
  104.  
  105. '*****************************************************************  
  106. ' function(公有)  
  107. ' 作用 :格式化遠程圖片地址爲本地位置  
  108. ' 參數 :imgurl(遠程圖片地址),imgfolder (本地圖片目錄),fristname(加入的前綴名稱)  
  109. '*****************************************************************  
  110. Public Function FormatImgPath(byref ImgUrl,byref ImgFolder,byref FristName,byref noimg)  
  111. strpath="" 
  112. ImgUrlImgUrl=ImgUrl  
  113. if instr(ImgUrl,"Nophoto") or lenb(GetData(ImgUrl,0))<=0 then  
  114. strpath=noimg 
  115. Response.Write "<a href="&strpath&">"&strpath&"</a>" &vbcrlf  
  116. else  
  117. if Instr(ImgUrl,".asp") then  
  118.    strpath=FristName&"_"&Mid(ImgUrl, InStrRev(ImgUrl, "=")+1)&".jpg"  
  119. else  
  120.    strpath=FristName&"_"&Mid(ImgUrl, InStrRev(ImgUrl, "/")+1)  
  121. end if  
  122. strpath = ImgFolder&"/"&strpath  
  123. strpath = Replace(strpath,"//","/")  
  124. if left(strpath,1)="/" then strpath=right(strpath,len(strpath)-1)  
  125. strpath = trim(strpath)  
  126. Response.Write "<a href="&strpath&">"&strpath&"</a>" &vbcrlf  
  127. savedata ImgUrl,strpath  
  128. end if  
  129. FormatImgPath = strpath 
  130. End function  

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章