如何从VBScript执行Javascript函数?

时间:2011-01-13 07:03:53

标签: windows internet-explorer vbscript wsh

我正在尝试使用WSH脚本从网页下载Excel文件

我的目标是将Excel文件从网页保存到我的计算机上。

到目前为止,我采取的步骤是:制作一个vbs文件,登录到https网页,使用第二个运行命令将我重定向到另一个页面,打开一个新标签,但之后我的知识有限。能够找到一个解决方案,如何从网站上的下载链接下载文件到我的硬盘驱动器上的位置。

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

现在,此时有一个下载链接,其中包含一个javascript函数javascript:download(parameters),在手动点击后会生成一个唯一的下载链接。

有什么方法可以使用任何Wscript下载它吗?我希望它可以与Windows 7和IE 7一起使用。我试过调查但它无济于事。

1 个答案:

答案 0 :(得分: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请求,但我假设服务器会接受您的用户名和密码作为MSXML2.XMLHTTP open电话的第4和第5个参数。

http.open "GET", URL, false, "username@", "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