Search code examples
excelvbaweb-scraping

Need to combine .getElementsByClass and .getElementsById to scrape data from website


I am writing a simple crawler using VBA. I found out that the data I am looking for correspond to the node <h6 class="country-name" id="Australia">.

I know that if I wanted to select data from, let us say, <div class="section-country">, I should use .getElementsByClassName("section-country") in my VBA macro.

In presence of both class and id in the node, which command should I insert in my VBA macro to scrape data?

Thanks a lot, Avitus

EDIT: writing .getElementsByClassName("country-name").getElementsById("Australia") gives me an error. Why?


Solution

  • getElementsByID (plural) doesn't exist - there should only be one item with a given ID. Therefore, use getElementByID (singular) which does exist. If there happen to be multiple elements with the same ID, this function will return the first one.

    As others have said, selecting by ID sounds more appropriate to what you want to do than selecting by class