Consulting

Results 1 to 6 of 6

Thread: Multiple errors in VBA trying to get HTML elements by Class & Tag

  1. #1

    Multiple errors in VBA trying to get HTML elements by Class & Tag

    I'm having a hard time getting information from web. Think I get the general concept but have trouble wrapping my head around things like Children and getElements.
    This will be used by multiple users so API isn't an option.
    I've reduced errors by a 1/3 via a week of research now I'm stuck.

    The items I need are highlighted:

    Capture.jpg

    Attached is my work so far with notes and errors in the comments. I tried a code wrap but kept getting "Post denied. New posts are limited by number of URLs it may contain and checked if it doesn't contain forbidden words"

    Thank you for your help and guidance!
    Attached Files Attached Files

  2. #2
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location


    Set idoc1 = Document.querySelectorAll("[info]")
    'For r = 1 To idoc1.Length - 1 '<- error: Invalid use of Null  (same error when r=0)
    MsgBox Len(idoc1)
    MsgBox idoc1.Count
    ReDim Results(1 To idoc.Children.Length, 1 To 6)
    Maybe
    ReDim Results(1 Toidoc.Children.Count, 1 To 6)
    Results(r, n) = ...
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  3. #3
    I'm afraid you're talking above my head. I'm not even sure my code is in the neighborhood to get what I want.
    Everything after the word Set is just guessing, wrongly judging from the number of errors happening.
    Thinking it'll also need a click to get to data-qa="daily" tab, but could be out of my depth there too
    Last edited by arcarc1309; 07-20-2021 at 11:29 AM.

  4. #4
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Yikes! You sure have selected a complex Program for your first attempt.

    First study http://www.snb-vba.eu/VBA_Arrays_en.html#L_3.1 to learn about Arrays. This project of yours will use Arrays a lot.

    Understand that each site must have custom Code to read it. You could open them each from the main sub, but IMO, it's just as well to open them in their own sub. It makes troubleshooting much easier.

    Preparationary code
    Option Explicit 'Should be the top line in all Modules
    
    'Seaweather and the Enum at the top of the main module are available to all code in modules
    Dim SeaWeather As Variant 'The Array to hold all information. Note all Names have a distinct meaning to your Project. 
    
    Enum SeaWeatherColumns 'Used to make the code more understandable and self checking.
       swcDay = 1 'Day is a VBA Keyword
        swcDate      'Date is a VBA Keyword. Note Enums are self incrementing. swcDate = 2
       swcHigh      'swc Prefix indentifies as Enum Variable
       swcLow
       swcPrecip . '% Sign not allowed
    End Enum
    This "Main" sub is complete as far as it goes (Only one Site ATT):
     Sub Main
    Dim i As Long
    Dim WeatherSite As String
    Dim SeaWeatherSites 'a Variant to hold the list of Web sites
    
      SeaWeaterSites =  Sheet1,Range(Range("A2", Cells(Rows.Count, "A").End(xlUp).Value 'Put the list into the Array
      ' Regardless of how many Sites in your list, we will start with only the First in the list: accuweather.com
    
      'For i = 1 to UBound(SeaWeatherSites) 'Not used at this stage of Programming
      i = 1 'Only used at this stage of Programming
       WeatherSite = SeaWeatherSites(i, 1) 'If the For was used. this will read all Sites in the list
    
       Select Case WeatherSite
          Case is = "https://www.accuweather.com/en/us/seattle/98104/weather-forecast/351409"
             AccuweatherSub WeatherSite
         'A Case Statement fo each Site
             'A sub for each Site. Pass WeatherSite to each
         Case Else
            GoTo PasteData
       End Select
      ' Next i  'Not used at this stage of Programming
    
    PasteData:
      Sheet2. Range("A1").Resize(Ubound(SeaWeather), 5) = Seaweather
    End Sub
    Except for the code to parse each Site, the code below can be copied for each site sub as called for in the Select Case in the Main sub
    Each Site Code should be in its own Module, In This case, I would put it in modAccuweater
    Private Sub AccuweatherSub(SiteName As String)
    Dim ArrTmp 'An Array to hold this sites data
    Dim ArrWeatherData
    Dim I As Long, j As Long, k As Long 'When Declared (Dimmed) Numerical variable are initialized to 0
    
    'Code to Open and parse SiteName and place its data into ArrTmp
    '
    'You will need to open a new thread and ask how to parse this (all) site(s) into ArrTmp
    'Include a link to this post for reference: http://www.vbaexpress.com/forum/showthread.php?69006-#post410534
    
    Redim ArrWeatherData(1 to UBound(ArrTmp) + Ubound(SeaWeather) + 1), 5) '+ 1 Assumes that ArrTmp is Lowerbound = 0
    For i = 1 to to Ubound(SeaWeather)
       ArrWeatherData(i, swcDay) = SeaWeather(i, swcDay)
       ArrWeatherData(i, swcDate) = SeaWeather(i, swcDate)
       ArrWeatherData(i, swcHigh) = SeaWeather(i, swcHigh)
       ArrWeatherData(i, swcLow) = SeaWeather(i, swcLow)
       ArrWeatherData(i, swcPrecip ) = SeaWeather(i, swcPrecip )
    Next i
    
    For J = i + 1 to UBoundArrTmp)
      k = k + 1
       ArrWeatherData(j, swcDay) = ArrTmp(k, ?)  '? must be determined for each Site
       ArrWeatherData(j, swcDate) = ArrTmp(k, ?) 'You may use CStr(Format(Date, "DDD")) for swcDay and Date for swcDate
       ArrWeatherData(j, swcHigh) = ArrTmp(k, ?)
       ArrWeatherData(j, swcLow) = ArrTmp(k, ?)
       ArrWeatherData(j, swcPrecip ) = ArrTmp(k, ? )
    Next j
    
    SeaWeather = ArrWeatherData
    End Sub
    Understand this code before opening another thread about Parsing websites.
    Last edited by SamT; 07-20-2021 at 02:13 PM.
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  5. #5
    WOW!!
    Thanks for the info and an idea where to start. I've got a lot of studying to do. Your help is much appreciated. Take care!

  6. #6
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    OOps! My bad. (Amazing what a few hours sleep will do)
    I forgot thwe we are assuming that that ArrTmp is Lowerbound = 0
      For J = i + 1 to UBoundArrTmp)
       ArrWeatherData(j, swcDay) = ArrTmp(k, ?)  '? must be determined for each Site
       ArrWeatherData(j, swcDate) = ArrTmp(k, ?) 'You may use CStr(Format(Date, "DDD")) for swcDay and Date for swcDate
       ArrWeatherData(j, swcHigh) = ArrTmp(k, ?)
       ArrWeatherData(j, swcLow) = ArrTmp(k, ?)
       ArrWeatherData(j, swcPrecip ) = ArrTmp(k, ? )
    
       k = k + 1
      Next j
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •