不幸的是,我並不十分熟悉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」。當然,那麼我必須將每個鏈接作爲單獨的「元素」或「項目」(不確定使用哪種數據格式,因爲字符串不會是答案)。感謝您解釋如何做對。