VBS从列表中打开多个URL并保存到文件

时间:2012-12-04 17:23:46

标签: vbscript

我需要打开多个网页,并将信息(只是文本)保存到每个网页的新文件中。我根本不具备VBScripts的经验。但是,我一直在寻找几天,这是我到目前为止收集的内容:

function download(sFileURL, sLocation, async)
  set objXMLHTTP = CreateObject("MSXML2.XMLHTTP")
  objXMLHTTP.open "GET", sFileURL, async
  on error resume next
  objXMLHTTP.send()
  if err.number = 0 then
    do until objXMLHTTP.Status = 200
     wscript.echo objXMLHTTP.Status
     wcript.sleep(200)
  loop
  if objXMLHTTP.Status = 200 Then
    set objADOStream = CreateObject("ADODB.Stream")
    objADOStream.Open
    objADOStream.Type = 1
    objADOStream.Write objXMLHTTP.ResponseBody
    objADOStream.Position = 0    
    set objFSO = Createobject("Scripting.FileSystemObject")
    If objFSO.Fileexists(sLocation) Then objFSO.DeleteFile sLocation
    Set objFSO = Nothing
    objADOStream.SaveToFile sLocation
    objADOStream.Close
    set objADOStream = Nothing
    download = true
   end if
 else
   download = false
 end if
   set objXMLHTTP = Nothing
 end function

 if download("http://books.google.com/books/feeds/volumes?q=isbn", "test.txt", false) then
   wscript.echo "download ok"
 else
   wscript.echo "download did not work"
 end if

如何更改

if download("http://books.google.com/books/feeds/volumes?q=isbn", "test.txt", false)

从文本文件中的列表中读取,并保存为test00,test01,test02,...

提前谢谢

1 个答案:

答案 0 :(得分:2)

这是最简单的代码,可以满足您的需求。

Set objFSO = CreateObject("Scripting.FileSystemObject")
ListFile = "D:\list.txt"
Set objFile = objFSO.OpenTextFile(ListFile)
FileNumber = 0
Do Until objFile.AtEndOfStream
    URLFromList = objFile.ReadLine
    FileNumber = FileNumber + 1
    if download(URLFromList, "test" & FileNumber & ".txt", false) then
        wscript.echo "download ok for " & URLFromList
    else
        wscript.echo "download did not work for " & URLFromList
    end if
Loop
objFile.Close
objFSO = Nothing