How to get the href value out from the given html code

  • Hi Everyone!

    I am quite new to web scraping and have a minor VBA knowledge. I would like to extract the href value/text from a parsed html code.

    The html code:

    And here is my VBA code which wants to get the href value:

    1. 'set the collection for those links which are in the "position_and_company" class
    2. Set my_data = .Document.getElementsByClassName("position_and_company").getElementsByTagName("a")i = 1
    3. For Each elem In my_data
    4. i = i + 1
    5. 'copy the data to the excel sheet
    6. ActiveSheet.Cells(i, 4).Value = elem.href
    7. ActiveSheet.Cells(i, 2).Value = elem.innerText
    8. Next

    So basically what I am trying to do is to get the href values (the links actually) only from those classes which are called "position_and_company".

    My problem is that the code is not working, each time I run it I get a runtime error: 438 (Object doesn't support this property or method) for the line below:

    1. Set my_data = .Document.getElementsByClassName("position_and_company").getElementsByTagName("a")

    What am I doing wrong? Why isn't this code working?

    Thank you in advance!

  • Re: How to get the href value out from the given html code

    getElementsByClassName returns a collection/array (the clue is the 's' for plural in 'Elements'), so you need to access a specific array element by its index number, or use a For Each loop.

    Try this rearranged code: