1 year ago

#369438

test-img

egreg

web scraping from google page no longer returns anything

The below Excel vba code use to work and return the market cap, 52 week low and current price into my spreadsheet. It no longer does though and cannot work out why. Class names haven't changed but the getElementsByClassName doesn't seem to return anything anymore I think.

Sub get_title_header()

    Dim wb As Object
    Dim doc As Object
    Dim sURL As String
    Dim i As Integer
    Dim allElements As IHTMLElementCollection
    
    Application.DisplayAlerts = False
    
    Set wb = CreateObject("internetExplorer.Application")
    sURL = "https://www.google.com/search?q=aapl+stock+quote"
    
    wb.navigate sURL
    wb.Visible = False
    
    While wb.Busy
        Application.Wait Now + #12:00:01 AM#
        DoEvents
    Wend
    
    Set doc = wb.document.body
    
    On Error GoTo err_clear
      
    Set allElements = doc.getElementsByClassName("iyjjgb")
    x = allElements(3).innerText
     Sheet6.Cells(i + 1, 2).Value = x
    x = allElements(8).innerText
     Sheet6.Cells(i + 1, 3).Value = x
    x = ""
    x = allElements(0).innerText
     Sheet6.Cells(i + 1, 4).Value = x
    x = ""
    
err_clear:
    If Err <> 0 Then
    Err.Clear
    Resume Next
    End If
    wb.Quit

End Sub

html

excel

vba

web-scraping

getelementsbyclassname

0 Answers

Your Answer

Accepted video resources