Hi everyone, maybe someone could help me!
I'm trying to identify the classname related with each string named "toSearch" in each URL. My logic is to loop every link and search for a text that is present in each level especified.
With my code below I'm able to identify classname of level1 and level2, but is not working for level3 and level4 and the other issue is that some cities don't have more than one location, in that case level4 exists but level3 doesn´t exist.
Then maybe someone could help me how to identify the classname for level3 and level4 taking in consideration the cases when there are only 3 levels (level1, level2, levell4) and if there is a way to give as input only the first URL and the macro be able to identify the other 3 as needed in each stage.
Level1 = Name of the state
Level2 = Name of the city
Level3 = Some text (location) in a link that is present for each city
Level4 = The street address (is not a link, but a text)
Thanks in advance
Code:
Sub GetClass()
Dim url1 As String, url2 As String, url3 As String, url4 As String
Dim toSearch1 As String, toSearch2 As String, toSearch3 As String, toSearch4 As String
Dim HTMLDoc As New HTMLDocument
'URL levels
url1 = "https://locations.bojangles.com/"
url2 = "https://locations.bojangles.com/al.html"
url3 = "https://locations.bojangles.com/al/huntsville.html"
url4 = "https://locations.bojangles.com/al/huntsville/11375-south-memorial-pkwy.html"
'Text to search in each level
toSearch1 = "Alabama"
toSearch2 = "Huntsville"
toSearch3 = "South Memorial Pkwy"
toSearch4 = "11375 South Memorial Pkwy"
'Print className for each level
Call LoopElements(url1, toSearch1, "Level1")
Call LoopElements(url2, toSearch2, "Level2")
Call LoopElements(url2, toSearch3, "Level3")
Call LoopElements(url4, toSearch4, "Level4")
End Sub
Function LoopElements(url As String, toSearch As String, level As String)
Dim HTMLDoc As New HTMLDocument
Dim links As Object
Dim i As Integer
With New ServerXMLHTTP60
.Open "Get", url, False
.send
HTMLDoc.body.innerHTML = .responseText
End With
Set links = HTMLDoc.body.getElementsByTagName("a")
With links
For i = 0 To .Length - 1
If .Item(i).innerText Like "*" & toSearch & "*" Then
Debug.Print level & " ClassName: " & .Item(i).className
End If
Next i
End With
End Function