Extract href link from source code using VBA

Jasvindra

New Member
Joined
Jul 31, 2018
Messages
35
Below is the source code which i am getting after browsing a website


<code style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: inherit; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; vertical-align: baseline; box-sizing: inherit; white-space: inherit;"><item><a href="/search/Listing/45678489?source=results" id="mk:0:mk"class="details">


</code>
I just want to copy link /search/Listing/45678489?source=results in excel and want to know how to click it
class="details" is same for all href links that i want copy while id keep on incrementing mk:1:mk, ms:2:mk and so on
 

Some videos you may like

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.

Domenic

MrExcel MVP
Joined
Mar 10, 2004
Messages
19,377
To get the href attribute, try...

Code:
htmlDoc.getElementById("mk:0:mk").getAttribute("href")

To click the link...


Code:
htmlDoc.getElementById("mk:0:mk").click

...where htmlDoc has been assigned an HTML document.

Hope this helps!
 

Jasvindra

New Member
Joined
Jul 31, 2018
Messages
35
Thanks for your reply Domenic. its working, however as i have mentioned in the original post ID keep on incrementing mk:1:mk, ms:2:mk and so on for each product listing. I want pull the url for each product .

1. Is there any wayout without using ID
2. I have been trying to run loop to pull link for all Ids for which below code has been showing error" Object variable or with block variable not set"
3. if running loop on ID is the only option. Could you please suggest how to set it for Last row of source code

Sub Scrap()


Dim IE As Object


Dim htmlDoc As HTMLDocument




Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.navigate "website url"
Do
DoEvents
Loop Until IE.readyState = READYSTATE_COMPLETE
Set htmlDoc = IE.document
For i = 0 To 93
erow = Sheets("Exec").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
Sheets("Exec").Cells(erow, 1) = htmlDoc.getElementById("mk:i:mk").getAttribute("href")

Next i


End Sub
 

Domenic

MrExcel MVP
Joined
Mar 10, 2004
Messages
19,377
If only the desired links have "details" as their class name, you can use the getElementsByClassName method...

Code:
    Dim htmlElement As MSHTML.IHTMLElement

    For Each htmlElement In htmlDoc.getElementsByClassName("details")
        Debug.Print htmlElement.getAttribute("href")
        htmlElement.Click
    Next htmlElement
 
Last edited:

Watch MrExcel Video

Forum statistics

Threads
1,114,002
Messages
5,545,439
Members
410,684
Latest member
LakTik
Top