PDA

View Full Version : [SOLVED:] Multiple errors in VBA trying to get HTML elements by Class & Tag



arcarc1309
07-19-2021, 10:23 AM
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. :crying:

The items I need are highlighted:

28754

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!

SamT
07-19-2021, 01:38 PM
:dunno


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) = ...

arcarc1309
07-20-2021, 10:57 AM
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 :banghead:

SamT
07-20-2021, 01:43 PM
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.

arcarc1309
07-20-2021, 03:50 PM
WOW!! :eek:
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!

SamT
07-20-2021, 07:33 PM
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