将所有来自HTML表格的项目存储到脚本字典中,并添加重复值

我想在一个字典中存储在html表中find的所有项目。

我有问题时,我有重复,因为我的下面的代码不再存储该项目,我需要从这个表中的所有项目,即使有任何重复。

如果我有像第38轮重复值,其他Match3具有相同的整数,我想再次列出这些重复值。

结果应该是这样的:

第38轮

MATCH1

MATCH2

第37轮

MATCH1

MATCH2

第38轮

MATCH3

Match4

…………..

Sub Get_URL_Addresses_test() Dim URL As String Dim ie As New InternetExplorer Dim HTMLdoc As HTMLDocument Dim dictObj As Object: Set dictObj = CreateObject("Scripting.Dictionary") Dim tRowID As String URL = "http://www.flashscore.ro/fotbal/anglia/premier-league-2015-2016/rezultate/" With ie .navigate URL .Visible = True Do Until .readyState = READYSTATE_COMPLETE: DoEvents: Loop Set HTMLdoc = .document End With For Each objLink In ie.document.getElementsByTagName("a") If Left(objLink.innerText, 4) = "Show" Or Left(objLink.innerText, 4) = "Arat" Then objLink.Click Application.Wait (Now + TimeValue("0:00:01")) objLink.Click Application.Wait (Now + TimeValue("0:00:01")) objLink.Click Application.Wait (Now + TimeValue("0:00:01")) 'Exit For End If Next objLink With HTMLdoc Set tblSet = .getElementById("fs-results") Set mTbl = tblSet.getElementsByTagName("tbody")(0) Set tRows = mTbl.getElementsByTagName("tr") With dictObj For Each tRow In tRows If tRow.getAttribute("Class") = "event_round" Then tRowClass = tRow.innerText 'MsgBox tRowClass If Not .Exists(tRowClass) Then .add tRowClass, Empty End If End If tRowID = Mid(tRow.ID, 5) If Not .Exists(tRowID) Then .add tRowID, Empty End If Next tRow End With End With i = 14 For Each Key In dictObj If Left(Key, 5) = "Runda" Or Left(Key, 5) = "Round" Then ActiveSheet.Cells(i, 2) = Key Else ActiveSheet.Cells(i, 2) = "http://www.flashscore.ro/meci/" & Key & "/#sumar-meci" End If i = i + 1 'MsgBox Key 'Debug.Print Key Next Key Set ie = Nothing MsgBox "Process Completed" End Sub 

您可以将项目存储在允许重复的通用容器中,如集合或数组。 但是既然你把它们存储在字典中,作为keys ,这可能意味着你以后想快速search一些项目的存在。 一个可能的解决scheme是“计数”每个项目(键)的出现次数,并将该数字存储在相应的值字段中。

 If tRow.getAttribute("Class") = "event_round" Then tRowClass = tRow.innerText dim n as Integer: n = dictObj.Item(tRowClass) ' creates and returns 0 if no exist yet dictObj.Item(tRowClass) = n + 1 End If 

稍后,您将能够检查字典中是否存在任何键,并且您还有该键的出现次数。

编辑

正如我所怀疑的那样,你使用的字典就像一个普通的容器,但是由于你想允许重复,Dictiobary不是你要去的地方。 只需使用一个Collection 。 以下是对代码的最小更改:

Set dictObj = CreateObject("Scripting.Dictionary") – > Set dictObj = new Collection

If Not .Exists(tRowClass) Then .add tRowClass, Empty End If

将上面的东西(3行)replace为:

 .add tRowClass 

而已。