2011-01-13 130 views
0

我想通過使用WSH腳本從網頁下載Excel文件如何從VBScript執行Javascript函數?

我的目標是將Excel文件從網頁保存到我的機器上。

到目前爲止,我採取的步驟是:製作一個vbs文件,該文件登錄到https網頁,使用第二個運行命令將我重定向到另一個頁面,打開一個新選項卡,但在此之後,我的有限知識isn無法找到解決方案,從而將文件從網站上的下載鏈接下載到硬盤上的某個位置。

Dim wshShell 
Set wshShell = CreateObject("WScript.Shell") 
WshShell.Run "URL", 9 
wscript.sleep 3000 
WshShell.SendKeys "[email protected]" 
WshShell.SendKeys "{tab}" 
WshShell.SendKeys "password" 
WshShell.SendKeys "{enter}" 
WshShell.Run "Another_URL" 

現在,在這一點上是具有javascript函數javascript:download(parameters)這在手動點擊生成一個唯一的下載鏈接下載鏈接。

有沒有什麼辦法可以使用任何Wscript下載?我希望它能夠與Windows 7和IE 7一起工作。我嘗試過調查它,但它沒有用。

回答

0

我用類似這樣

option explicit 

Const URL = "http://url/to/file.xls" 
Const adTypeBinary = 1 
Const adSaveCreateOverWrite = 2 

' request the file over http 
dim http: set http = CreateObject("MSXML2.XMLHTTP") 
http.open "GET", URL, false 
http.send 

' write the response text to a binary file 
dim stream: set stream = CreateObject("ADODB.Stream") 
stream.type = adTypeBinary 
stream.open 
stream.write http.responseBody 
stream.SaveToFile "output.xls", adSaveCreateOverWrite 
stream.close 

腳本取得了一些成功,雖然我還沒有使用它的HTTPS請求,我假設服務器將接受您的用戶名和密碼4和第五個參數調用MSXML2.XMLHTTPopen

http.open "GET", URL, false, "[email protected]", "password" 

我已經嘗試過了,它肯定工作在一個普通的http請求

看到http://msdn.microsoft.com/en-us/library/ms759148(VS.85).aspx的HTTP請求和http://msdn.microsoft.com/en-us/library/ms675032(VS.85).aspx爲ADODB流


很好地替代上述可能使用Internet Explorer自動化對象。我不知道你會如何處理文件下載,但下面的代碼片段可能會給你一個起點

option explicit 

' create an instance of IE 
dim ie: set ie = CreateObject("InternetExplorer.Application") 
' load a url 
ie.Navigate "http://stackoverflow.com/questions/4677595/how-can-i-execute-a-javascript-function-from-vbscript" 
' sleep while IE loads the content 
do while ie.busy 
    WScript.Sleep 10 
loop 
'access the document object 
dim doc: set doc = ie.document 

' have IE natvigate to a link on the downloaded page. this could be your 
' download link perhaps? 
ie.Navigate doc.anchors(0).href 
' wait while the new page loads... 
do while ie.busy 
    WScript.Sleep 10 
loop 
' output the new content 
WScript.Echo doc.documentElement.innerHTML 
' close IE 
ie.Quit 
+0

這是產生HTTPS鏈接:// Fixed_URL/Random_No_Generated _tbldnld =(1 PARAM)? &sc =(fn的第2個參數)手動發生這種情況,單擊下載後會打開一個FILE下載框,然後單擊保存將其保存到一個位置。 URL沒有文件名,但名稱與隨機號相同。那麼,我可以模仿這種行爲嗎,如果是的話,是否有可能使用腳本或任何程序必須編寫 - 請指導我,如果不是,爲什麼不可能。感謝您認證的oracle專業人士的幫助,但我看不到如何解決這個問題。 – tangyorangesour 2011-01-13 10:43:00