Extract href link from source code using VBA

Jasvindra

New Member
Joined
Jul 31, 2018
Messages
38
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
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
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!
 
Upvote 0
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
 
Upvote 0
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:
Upvote 0

Forum statistics

Threads
1,224,813
Messages
6,181,117
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top