2011-01-13 227 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