Extract href link from source code using VBA

Jasvindra

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

Domenic

MrExcel MVP
Joined
Mar 10, 2004
Messages
18,941
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
34
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
18,941
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:

Forum statistics

Threads
1,078,442
Messages
5,340,305
Members
399,366
Latest member
ahmed elsaid

Some videos you may like

This Week's Hot Topics

  • Problem with Radio Button's format control
    I am creating an employee evaluation template (a sample is below) Column A is the category Column B, C D, E and F will be ratings (unacceptable...
  • Last Display on userform to a Listbox
    [CODE=vba] lstdisplay.ColumnCount = 15 lstdisplay.RowSource = "A1:O600000" [/CODE] So when i do this it Displays everything on the sheet i am...
  • Rename and move files to a new location
    Dear all, I have an excel file with the following information. The actual file name is at column A but i want to rename it using the following...
  • Help with True/False Formula
    Hello! Am stumped how to fix this formula, in which my result returns 'True', but it should return False. =IF(AG2=True...
  • Clear extra characters from a provided range of cells
    Dear All, I have following code which gives me desired output to remove extra characters from a provided range. But it takes too much time when...
  • Help with Current and highest streaks
    Hi there, I've just joined the forum and this is my first post. I've already spent quite a bit of time searching the net and this forum for a...
Top