I'm very much new to the VBA programming. I have a list of keywords that are to be sourced in a certain website and I want their sourced links as the end result. I want this to be happened for all the keywords in my list using a VBA program. I tried this with below code, but I'm not getting the desired result. Please advise where am I going wrong.
Sub GetCanonicalURL()
On Error Resume Next
Dim ie As New SHDocVw.InternetExplorer
Dim mykeyword As String
Dim result As String
Dim lastrow As Integer
Dim mylinks As Object
Dim mylink As Object
lastrow = Sheet1.Cells(Rows.Count, "A").End(xlUp).Row
For i = 2 To lastrow
mykeyword = Sheet1.Cells(i, 1).Value
ie.Visible = True
ie.navigate "https://en.wikipedia.org/wiki/Home_page"
Do While ie.readyState <> READYSTATE_COMPLETE
Loop
ie.document.forms("searchform").elements("searchInput").Value = mykeyword
ie.document.forms("searchform").elements("searchButton").Click
result = ie.document.body.innerHTML
Set HTML = CreateObject("htmlfile")
HTML.body.innerHTML = result
Set mylinks = HTML.getElementsByTagName("link")
For Each mylink In mylinks
If mylink.hasAttribute("canonical") Then
Sheet1.Cells(i, "B").Value = mylink
Next mylink
If i = lastrow Then
ie.Quit
End If
Next i
End Sub
In the above code, I'm stuck in that empty part of the program (yellow color highlighted - see screenshots), confused on which strategy to use to get the canonical URL of each sourced websites. Please see the screenshots of the images attached below.
canonical href link:
[1