title: Web Scraping by Elements
body: <p>I'm trying to scrape the data from a web-page. It won't work for a website for which all the format is same like as class, tag everything. I am getting an error is "Subscript out of range" and it's highlighting on "ReDim results(1 To rowCount, 1 To numColumns)" code.</p>
  
  <p>I got an answer on page: <a href="https://stackoverflow.com/questions/55549841/web-scraping-by-tagname?noredirect=1#comment97801463_55549841">Web Scraping by TagName</a>
  the code works fine for <a href="https://www.neighborhoodselfstorage.net/self-storage-ocean-city-md-88769" rel="nofollow noreferrer">https://www.neighborhoodselfstorage.net/self-storage-ocean-city-md-88769</a></p>
  
  <p>Now I am trying to use the same code for: <a href="https://www.stormore.net/self-storage-seattle-wa-101616#utm_source=GoogleLocal&amp;utm_medium=WRLocal&amp;utm_campaign=101616" rel="nofollow noreferrer">https://www.stormore.net/self-storage-seattle-wa-101616#utm_source=GoogleLocal&amp;utm_medium=WRLocal&amp;utm_campaign=101616</a></p>
  
  <p>Please anybody help to solve this problem.</p>
  
  <pre><code>Option Explicit  
  Public Sub GetInfo()
  Dim ws As Worksheet, html As HTMLDocument, s As String
  Const URL As String = "https://www.stormore.net/self-storage-seattle-wa-101616#utm_source=GoogleLocal&amp;utm_medium=WRLocal&amp;utm_campaign=101616"
  
  Set ws = ThisWorkbook.Worksheets("Sheet1")
  Set html = New HTMLDocument
  With CreateObject("MSXML2.XMLHTTP")
      .Open "GET", URL, False
      .setRequestHeader "User-Agent", "Mozilla/5.0"
      .send
      s = .responseText
      html.body.innerHTML = s
  
      Dim headers(), results(), listings As Object, amenities As String
  
      headers = Array("Size", "Description", "Amenities", "Offer1", "Offer2", "RateType", "Price")
      Set listings = html.querySelectorAll(".main li[class]")
  
      Dim rowCount As Long, numColumns As Long, r As Long, c As Long
      Dim icons As Object, icon As Long, amenitiesInfo(), i As Long, item As Long
  
      rowCount = listings.Length
      numColumns = UBound(headers) + 1
  
      ReDim results(1 To rowCount, 1 To numColumns)
      Dim html2 As HTMLDocument
      Set html2 = New HTMLDocument
      For item = 0 To listings.Length - 1
          r = r + 1
          html2.body.innerHTML = listings.item(item).innerHTML
          'size,description, amenities,specials offer1 offer2, rate type, price
  
          results(r, 1) = Trim$(html2.querySelector(".size").innerText)
          results(r, 2) = Trim$(html.querySelector(".description").innerText)
          Set icons = html2.querySelectorAll("i[title]")
  
          ReDim amenitiesInfo(0 To icons.Length - 1)
  
          For icon = 0 To icons.Length - 1
              amenitiesInfo(icon) = icons.item(icon).getAttribute("title")
          Next
  
          amenities = Join$(amenitiesInfo, ", ")
  
          results(r, 3) = amenities
          results(r, 4) = html2.querySelector(".offer1").innerText
          results(r, 5) = html2.querySelector(".offer2").innerText
          results(r, 6) = html2.querySelector(".rate-label").innerText
          results(r, 7) = html2.querySelector(".price").innerText
      Next
  
      ws.Cells(1, 1).Resize(1, UBound(headers) + 1) = headers
      ws.Cells(2, 1).Resize(UBound(results, 1), UBound(results, 2)) = results
  End With
  End Sub
  </code></pre>