Scrape text from a website using Excel VBA

后端 未结 2 2000
悲哀的现实
悲哀的现实 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:55

    If you just need to get the content of the webpage in plain text this code is more concise

    Function WEBSITE_TEXT(Destination As String) As String
    ' Requires a reference to Microsoft XML, v6.0
    ' Draws on the stackoverflow answer at bit.ly/parseXML
    Dim myRequest As XMLHTTP60
    Dim myDomDoc As DOMDocument60
    
        ' Check and clean inputs
        On Error GoTo exitRoute
    
        If Destination = "" Then
          WEBSITE_TEXT = ""
         Exit Function
        End If
    
        ' Read the XML data from the Google Maps API
        Set myRequest = New XMLHTTP60
        myRequest.Open "GET", Destination, False
        myRequest.send
    
        ' Parse HTML content
        Dim html As New HTMLDocument
        Dim text As String
        html.body.innerHTML = myRequest.responseText
    
        ' Return the website content
        text = html.body.innerText
        If Not html Is Nothing Then WEBSITE_TEXT = text
    exitRoute:
        ' Tidy up
        text = ""
        Set myRequest = Nothing
    End Function
    

提交回复
热议问题