简体   繁体   中英

Excel 2010 Download CSV with dynamic name from secure website

I am wanting to login to a site, navigate to a page, then download the .CSV file that will always end with a dynamic string due to it being 'custom'.

I have tried to access the site by recording a macro. However, as the data is not in a table the macro recorder is not able to pick up the actual address of the .csv file.

The display text is always:

Results [link]Click to Download[/link]  

The html values are always:

<td class="smallText"><b>Results</b> <a href="vendor_report.php?report=custom [insert extremely long string here] ><u>Click to Download</u></a></td>   

Without using a table, is there a way to get to this .csv & save it to my PC?
I am aware that the use of <td> denotes it is part of a table, but it is definitely not picking it up, I've gone through the site using the macro recorder and it's not picking up the inner contents from the page.
https://[domain].php?vf=vieworders

I had also thought to navigate to the site page, highlight the text, copy & paste to a spare sheet in my book, then use some code L42 previously wrote here (below) however I can't even get the copy & paste to work correctly.

For Each hlink In ThisWorkbook.Sheets("NameOfYourSheet").Hyperlinks
    Set wb = Workbooks.Open(hlink.Address)
    wb.SaveAs saveloc & hlink.Range.Offset(0,1).Value & ".xlsx"
    wb.Close True
    Set wb = Nothing
Next

Please advise. Thank you in advance.

UPDATE
I have found which table this is hiding in, Table 2 . It is however in the midst of a lot of other text.
When I have copied & pasted the table contents to my sheet, I have problems getting the link to show as it's HTML value so I can then use that with my 2nd option (open links from spreadsheet).
This could be an issue with the original Get Data code I am using.

This is how it looks. The cells either side are filled, as well as that huge chunk of (blanked out) text in B20

Could Regex be of use here??

来自web tabledownload的大块数据

You could try using the XMLHTTP object with a stream:

Sub SO()

Dim objStream As Object, strURL As String

Set objStream = CreateObject("ADODB.Stream")
strURL = "vendor_report.php?report=custom [insert extremely long string here]"

With CreateObject("Microsoft.XMLHTTP")
    .Open "GET", strURL, False
    .Send
    If .Status = 200 Then
        objStream.Open
        objStream.Type = 1
        objStream.Write .ResponseBody
        objStream.SaveToFile "C:\users\bloggsj\output.csv", 2
        objStream.Close
    End If
End With

Set objStream = Nothing

End Sub

Change the save path as required.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM