我希望有人可以提供以下帮助。我正在尝试使用VBA自动从网站下载文件。
我正在使用以下代码,但是这会将csv文件保存到所需的路径中。我要做的是“单击”下载链接,然后将文件保存到指定的路径。
网页上的检查元素信息如下:
input type="hidden" name="download" value=""
a href="#" onclick="var dl_var = find_control('download'); dl_var.value = '1'; submitFormByPost(dl_var.form); dl_var.value = ''; return false;">
<img src="/i/prices_export_excel.gif" height="48" width="48" alt="Download to spreadsheet"><br>
download to spreadsheet
</a
我尝试查看各种不同的链接,但是似乎都没有解决方案。 我的代码在下面。
Sub DownloadFile()
Dim myURL As String
myURL = "https://YourWebSite.com/?your_query_parameters"
Dim WinHttpReq As Object
Set WinHttpReq = CreateObject("Microsoft.XMLHTTP")
WinHttpReq.Open "GET", myURL, False, "username", "password"
WinHttpReq.send
myURL = WinHttpReq.responseBody
If WinHttpReq.Status = 200 Then
Set oStream = CreateObject("ADODB.Stream")
oStream.Open
oStream.Type = 1
oStream.Write WinHttpReq.responseBody
oStream.SaveToFile "C:\file.csv", 2 ' 1 = no overwrite, 2 = overwrite
oStream.Close
End If
End Sub`
我想将文件下载到所需的文件路径,而不是保存页面。
答案 0 :(得分:0)
我有自己的解决方案,可以从网页下载文件并正常工作。
我的代码示例
Dim htmlDoc As HTMLDocument ' global variable
Dim myHtmlElement As IHTMLElement 'global variable
Sub test()
Dim dominio As String
dominio = "your url"
Dim ie As InternetExplorer
Set ie = New InternetExplorer
ie.Visible = True
ie.navigate dominio
'Do
'DoEvents
'Loop Until ie.readyState = READYSTATE_COMPLETE
Do While ie.Busy
Application.Wait DateAdd("s", 1, Now)
Loop
Set htmlDoc = ie.document
Call ie.document.getElementById("tag_id_username").setAttribute("value", "your username")
Call ie.document.getElementById("tag_id_password").setAttribute("value", "your password")
ie.document.getElementById("loginForm").Click 'button login
While ie.readyState <> READYSTATE_COMPLETE
DoEvents
Wend
Call ie.document.getElementById("link_document_download").Click
Do While ie.Busy
Application.Wait DateAdd("s", 1, Now)
Loop
Application.SendKeys "%{S}" ' download the file when is open the pop up
Set ie = Nothing
End Sub
此解决方案要求您必须保持打开状态并集中浏览器的位置,否则application.SendKeys "{S}"
不起作用。
如果没有id标签,则可以使用类名(设置元素= ie.Document.querySelector(".classname"))
...
此文件位于下载文件夹(defalut)中。 您可以阅读这篇文章如何更改路径。 How can I change the file path of a Workbook in VBA?
希望这会有所帮助
评论后编辑帖子
记住要添加此参考
以下是我在您的链接和“作品”中尝试过的代码。使用此代码,您可以输入输入标签用户名-密码,然后单击按钮登录您的帐户。在我们必须编写下一个代码以单击下载文件之后...
Sub test()
Dim dominio, value As String
Dim myInput As Object
dominio = "https://www.steelbb.com"
Dim ie As InternetExplorer
Set ie = New InternetExplorer
ie.Visible = True
ie.navigate dominio
Do While ie.Busy
Application.Wait DateAdd("s", 1, Now)
Loop
For Each myInput In ie.document.getElementsByTagName("input") 'Cells
If myInput.Name = "username" Then
Call myInput.setAttribute("value", "write your username")
ElseIf myInput.Name = "password" Then
Call myInput.setAttribute("value", "write your password")
ElseIf myInput.Name = "doLogin" Then
'Application.Wait DateAdd("s", 1, Now)
Call myInput.Click
End If
Next myInput
'Add next code after login
Set ie = Nothing
End Sub
第二次编辑
如果以上代码对您有用,请在注释后添加此代码 '登录后添加下一个代码 '----------------------下载文件
'Add next code after login
Do While ie.Busy
Application.Wait DateAdd("s", 1, Now)
Loop
For Each myInput In ie.document.getElementsByTagName("a")
If myInput.href = "#" And myInput.onclick = "var dl_var = find_control('download'); dl_var.value = '1'; submitFormByPost(dl_var.form); dl_var.value = ''; return false;" Then
myInput.Click
' wait 2 seconds we give the time to load the file and only when there is the pop up we can click save button
Application.Wait DateAdd("s", 2, Now)
' download the file when is open the pop up
Application.SendKeys "%{S}"
Exit For
End If
Next myInput
Set ie = Nothing
End Sub
我尝试了此代码,并在您的帖子中显示了您的输入信息
输入您的帖子
网页上的检查元素信息如下:
input type =“ hidden” name =“ download” value =“”
a href =“#” onclick =“ var dl_var = find_control('download'); dl_var.value ='1'; SubmitFormByPost(dl_var.form); dl_var.value =''; 返回false;“>
下载到电子表格
答案 1 :(得分:0)
</td>
<td>
<table width=100%>
<tr>
<td style="width:20px;"></td>
<td align="center" width="100">
<input type="hidden" name="download" value="" />
<a href="#" onclick="var dl_var = find_control('download'); dl_var.value = '1'; submitFormByPost(dl_var.form); dl_var.value = ''; return false;">
<img src="/i/prices_export_excel.gif" height="48" width="48" alt="Download to spreadsheet" /><br />
download to spreadsheet
</a>
</td>
<td style="width:20px;"></td>
<td align="center" width="100">
<a href="#" onclick="graph_print(AmCharts.getExport(jQuery('#amcharts2_container').get(0))[0]); return false;">
<img src="/i/prices_graph_print.gif" height="48" width="51" alt="Print graph" /><br />
print graph
</a>
</td>
<td style="width:20px;"></td>
<td align="center" width="100">
<a href="#" onclick="graph_download(AmCharts.getExport(jQuery('#amcharts2_container').get(0))[0]); return false;">
<img src="/i/prices_export_image.gif" height="48" width="51" alt="Export image" /><br />
export image
</a>
</td>
</tr>
</table>
</td>
</tr>
</table>
<div id="amcharts2_container" style="z-index: -1; width: 780px; height: 550px;"></div>
<script type="text/javascript">
AmCharts.ready(function(){