2017-08-25 69 views
1

不幸的是,我並不十分熟悉HTML代碼(標籤,類,ID等)的任何「組件」以及它們之間的區別。 我想通過鏈接列表運行一個程序,單擊它們中的每一個,然後在「clicked-on」頁面上找到任何下載鏈接。 我的示例URL是「https://c64g.com/games/」,我已經設法運行鏈接列表並讓VBA打開每個鏈接。但是,我不能讓VBA識別頁面上有多少文件可供下載,因爲我無法從html中獲取這些「元素」。VBA - 如何運行下載鏈接列表(網頁掃描)

例(選擇頁 「1942年」):

<h1 class="c64u px16">Free C64 Game Download</h1> 
<form action="/games/download/get/12" method="post"> 
    <button class="btn-link" type="submit">Download 1942.Elite.+2-BAM.zip (31K)</button> 
</form> 
<form action="/games/download/get/13" method="post"> 
    <button class="btn-link" type="submit">Download 1942.Elite.+7hpd-REM.zip (38K)</button> 
</form> 
<form action="/games/download/get/14" method="post"> 
    <button class="btn-link" type="submit">Download 1942.Elite.CFO.zip (27K)</button> 
</form> 
<form action="/games/download/get/15" method="post"> 
    <button class="btn-link" type="submit">Download 1942_v1.Capcom.+2-MHI.zip (25K)</button> 
</form> 
<form action="/games/download/get/16" method="post"> 
    <button class="btn-link" type="submit">Download 1942_v2.Capcom.+2p-MHI.zip (37K)</button> 
</form> 
<script type="text/javascript"><!-- 

VBA代碼:

Sub useClassnames() 

Dim ie As Object 
Dim internetlink As Object 
Dim internetinnerlink As Object 

Set ie = CreateObject("InternetExplorer.Application") 

ie.Visible = True 
ie.navigate ("https://c64g.com/games/") 

Do While ie.readyState <> READYSTATE_COMPLETE 
    Application.StatusBar = "Loading Web page..." 
    DoEvents 
Loop 


Set internetlink = ie.document.getElementsByTagName("a") 
i = 0 

For Each internetinnerlink In internetlink 
'for testing purposes I want only to open the page for "1942", later all links will be run through 
    If internetinnerlink.innerText = "1942" Then 
     internetinnerlink.Click 
     Exit For 
    End If 
    i = i + 1 
Next internetinnerlink 

Dim alldownloads As Object 
'I have tried any kind of name ("btn-group", "button class", btn-link") and element fetch code (by Id, by name, by tagname, by classname) 
Set alldownloads = internetlink(i).getElementsByClassName("btn-group") 
MsgBox "alldownloads.Length: " & alldownloads.Length 

'ie.Quit 

End Sub 

任何幫助是真正的讚賞,尤其是解釋爲什麼我不能搶整頁文本(例如作爲一個字符串),並讓VBA通過它直到找到單詞「btn-link」。當然,那麼我必須將每個鏈接作爲單獨的「元素」或「項目」(不確定使用哪種數據格式,因爲字符串不會是答案)。感謝您解釋如何做對。

回答

0

我不知道你爲什麼引用internetlink(i)它是前一頁中含有「a」標籤名稱的元素的前一個元素的數組,你應該使用已加載的IE對象新頁面包含下載鏈接,從html正文中獲取鏈接,然後根據你的需要做任何事情。

在這個例子中,我會告訴你如何只從你問的那個網頁下載一個文件。

只需閱讀本代碼中的註釋即可。

下載鏈接是上部的形式操作的按鈕元素,所以我們把每個BTN-Link類,並與他們的父母,我們得到表單動作

因爲下載(下載文件的鏈接) zip我們必須做一個POST請求,我們必須使用SHDocVw.InternetExplorer對象的Navigate方法來發布一些無關緊要的東西,所以我創建了一個測試字符串轉換爲字節,將其發佈到請求中,然後導航窗口下載項目應該在IE中彈出。

試試看。

Sub useClassnames() 

Dim ie As Object 
Dim internetlink As Object 
Dim internetinnerlink As Object 

Set ie = CreateObject("InternetExplorer.Application") 

ie.Visible = True 
ie.navigate ("https://c64g.com/games/") 

Do While ie.readyState <> READYSTATE_COMPLETE 
    Application.StatusBar = "Loading Web page..." 
    DoEvents 
Loop 


Set internetlink = ie.document.getElementsByTagName("a") 
i = 0 

For Each internetinnerlink In internetlink 
    If internetinnerlink.innerText = "1942" Then 
     internetinnerlink.Click 
     Exit For 
    End If 
    i = i + 1 
Next internetinnerlink 


Do While ie.readyState <> READYSTATE_COMPLETE 
    Application.StatusBar = "Loading Web page..." 
    DoEvents 
Loop 

Set alldownloads = ie.document.getElementsByClassName("btn-link") 
MsgBox "alldownloads.Length: " & alldownloads.Length 
'example with 1 link, you should create an array of them then cycle it, i commented the for each out, you could decomment and use itm to get every btn-link 
'For Each itm In alldownloads 
    Set tst = alldownloads(0).parentelement 'parent of the button where the form post data is, with the link to the download 
    link = tst.Action 'taking the download link 
'Next itm 


'Now i create and example data to post, we cannot use POST Method in InternetExplorer.Application without posting anything. 
FormData = "test" 
Dim bFormData() As Byte 
ReDim bFormData(Len(FormData) - 1) 
bFormData = StrConv(FormData, vbFromUnicode) 

'Posting the request to the previous link, now a window with the attempt to download the file should open in IE. 
ie.navigate link, Empty, Empty, bFormData 

ie.Quit 

End Sub