shahin
Active Member
I've written some code in vba to pull out doctor names, their phone numbers and email addresses from a webpage with load more button. The way I've written my script is just doing awesome with no issues. After a long trial I could find out that lazy-load can be controlled in a very easy way. However, the only thing I would like to improve upon my existing script is use any method to avoid hardcoded loop so that it will keep going down while clicking on the load more button until there is no such button left to be clicked. At this moment, If I make my loop "For scroll_down = 1 To 20" to "For scroll_down = 1 To 50" then it will no doubt reaches the bottom but i would like to avoid this hardcoded loop. Any help will be highly appreciated.
This is the script:
This is the script:
Code:
Sub Web_Data()
Dim IE As New InternetExplorer, html As HTMLDocument
Dim storage As Object, posts As Object
With IE
.Visible = False
.navigate "http://www.physiofirst.org.uk/find-physio/search-physio.html"
Do While .readyState <> READYSTATE_COMPLETE: Loop
Set html = .document
End With
For scroll_down = 1 To 10
Set storage = html.getElementsByClassName("articles-item")
html.parentWindow.scrollBy 0, 99999
html.getElementById("load-more-practice").Click
Application.Wait Now() + TimeValue("00:00:005")
Next scroll_down
For Each posts In storage
With posts.getElementsByClassName("heading")
If .Length Then Row = Row + 1: Cells(Row, 1) = .Item(0).innerText
End With
With posts.getElementsByClassName("no-list")(0).getElementsByTagName("li")
If InStr(1, posts.innerText, "Tel:", 1) > 0 Then Cells(Row, 2) = Split(.Item(0).innerText, "Tel:")(1)
End With
With posts.getElementsByClassName("no-list")(1).getElementsByTagName("a")
If InStr(1, posts.innerText, "Email us", 1) > 0 Then Cells(Row, 3) = Split(.Item(0).href, "mailto:")(1)
End With
Next posts
IE.Quit
End Sub
Last edited: