Scrape text from a website using Excel VBA

后端 未结 2 1980
悲哀的现实
悲哀的现实 2021-01-14 05:01

I found this article explaining how to scrape certain tags from a website using Excel VBA.

The code below gets the content from the first

tag

2条回答
  •  有刺的猬
    2021-01-14 05:54

    You're almost there! doc.GetElementsByTagName("p") returns a collection of HTMLParagraphElement objects of which you accessed the first entry using doc.GetElementsByTagName("p")(0). As you allude to, a For Each loop would let you access each in turn:

    Sub get_title_header()
    Dim wb As Object
    Dim doc As Object
    Dim sURL As String
    Dim lastrow As Long
    Dim i As Integer
    lastrow = Sheet1.Cells(Rows.Count, "A").End(xlUp).Row
    
    For i = 2 To lastrow
    Set wb = CreateObject("internetExplorer.Application")
    sURL = Cells(i, 1)
    
    wb.navigate sURL
    wb.Visible = True
    
    While wb.Busy
        DoEvents
    Wend
    
    'HTML document
    Set doc = wb.document
    
    Cells(i, 2) = doc.Title
    
    On Error GoTo err_clear
    
    Dim el As Object
    For Each el In doc.GetElementsByTagName("p")
        Cells(i, 3).Value = Cells(i, 3).Value & ", " & el.innerText
    Next el
    
    err_clear:
    If Err <> 0 Then
    Err.Clear
    Resume Next
    End If
    wb.Quit
    Range(Cells(i, 1), Cells(i, 3)).Columns.AutoFit
    Next i
    
    End Sub
    

提交回复
热议问题