0

Using the below script I'm successfully returning values from the HTML tables into the workbook from the following links: link1 and link2. But when I'm trying to use the same script for the following link3, it does not return anything back. I think it is due to complex HTML table structure existing on the website. I believe code requires .Item(0) number to be adjusted due to table complexity, please advice.

Sub Web_Data()
    Dim http As New XMLHTTP60, html As New HTMLDocument
    Dim topic As HTMLHtmlElement

    With http
        .Open "GET", "http://www.dolphinfitness.co.uk/en/optimum-nutrition/", False
        .send
        html.body.innerHTML = .responseText
    End With

    For Each topic In html.getElementsByClassName("category-products")
        With topic.getElementsByClassName("product-name")
            If .Length Then x = x + 1: Cells(x, 1) = .Item(0).innerText
        End With
        With topic.getElementsByClassName("price")
            If .Length Then Cells(x, 2) = .Item(0).innerText
        End With
    Next topic
End Sub
10
  • Could you please make up the script and I will download selenium.
    – Martin
    Commented Jul 28, 2017 at 21:44
  • Which version is it that I'm required to install to make this work? selenium download page
    – Martin
    Commented Jul 28, 2017 at 21:46
  • Btw, I took out my comment seeing the link in your script. Is this the link you need to scrape?
    – SIM
    Commented Jul 28, 2017 at 21:48
  • This is the link that I want to scrape off. but essentially I want to build a database of all three websites to see the prices in one place. But that the link mentioned in this comment that script doesnt work.
    – Martin
    Commented Jul 28, 2017 at 21:50
  • 1
    Your script is ready and i got it working but you won't be able to run it now.
    – SIM
    Commented Jul 28, 2017 at 22:02

1 Answer 1

1

The very site you mentioned in your post is a bit tricky when it comes to parse the price of different products. Few products have got original price with it and the rest have got special price with them. You can't parse both of them all at once until you apply a technique with your expression. I've written an xpath which is able to deal with them and you will be able to get them all. Here is the script:

Sub Body_Building()
    Dim driver As New WebDriver, post As Object

    With driver
        .Start "chrome", "http://www.bodybuildingwarehouse.co.uk"
        .Get "/optimum-nutrition?limit=all"
    End With

    On Error Resume Next
    For Each post In driver.FindElementsByClass("grid-info")
        i = i + 1: Cells(i, 1) = post.FindElementByClass("product-name").Text
        Cells(i, 2) = post.FindElementByXPath(".//span[@class='regular-price']//span[@class='price']|.//p[@class='special-price']//span[@class='price']").Text
    Next post
End Sub

Let me know if you have any problem executing the script. Btw, selenium binding with vba doesn't have any property to shun "On error resume next" so i put it before the loop. Thanks.

17
  • Ok, I have got the idea of how it is done. But I'm getting and error when trying to run the above VBA highlighting the following: driver as New ChromeDriver and the error itself: User defined type not defined.
    – Martin
    Commented Jul 29, 2017 at 15:03
  • Two things come in my mind to see this. 1. have you installed chromedriver? 2. did you add "selenium type library" in the reference library before execution? Btw, web scraping will be very comfortable if you know how to use selenium cause nowadays you will face lots of sites with javascript injected.
    – SIM
    Commented Jul 29, 2017 at 15:09
  • Sorry that I'm bothering you so much with this but this is completely new thing for me. I'm getting the following error: imgur I have added selenium to the library references so as I have installed ChromeDrivers following instructions on this link
    – Martin
    Commented Jul 29, 2017 at 17:12
  • No problem Martin. Ignore the chromedriver thing for a while. Just tell me- can you run any script using selenium with vba as you said earlier, I meant have you been able to run anything with selenium?
    – SIM
    Commented Jul 29, 2017 at 17:48
  • I'm not entirely sure what do you exactly mean by saying can I run scripts using selenium. If I run web_data sub on BodyBuildingWarehouse website, VBA successfully scrapes HTML just its not right values returned as there is multiple price table id's. Before I have installed selenium this same web_data script was returning nothing.
    – Martin
    Commented Jul 29, 2017 at 17:58

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.