Home » excel » Scraping current date from website using Excel VBA

Scraping current date from website using Excel VBA

Posted by: admin May 14, 2020 Leave a comment

Questions:

Error
enter image description here

Librarys
enter image description here

I need the date of the current day. I do not want to place it inside a variable to be able to have it work, instead I would like that variable to be Date or in its default String.

Sub WEB()

    Dim IE As Object
    Dim allelements As Object

    Application.ScreenUpdating = False
    Set IE = CreateObject("InternetExplorer.Application")

    IE.navigate "http://www.fechadehoy.com/venezuela"
    Do Until IE.ReadyState = 4
        DoEvents
    Loop
    Application.Wait (Now + TimeValue("0:00:01"))
    IE.document.getElementById ("date")
    IE.Visible = True
    Set IE = Nothing
    Application.ScreenUpdating = True

End Sub

The website is http://www.fechadehoy.com/venezuela

I only need the date of this page. I am not interested in any other element of the macro.

I just need to extract the current date and get it in a variable.

How to&Answers:

Although the answer given by @Siddharth Rout is perfectly fine, it would require quite a bit of string manipulation to get the date in a usable form.

For the above reason I’m providing an alternative solution which gets the date in a directly usable format, ready to be manipulated and used in further calculations if necessary.

As a bonus I am demonstrating how to get the date using an HTTP request instead of using the Internet Explorer. This makes the code more efficient.

Option Explicit

Sub getDate()
Dim req As New WinHttpRequest
Dim doc As New HTMLDocument
Dim el As HTMLParaElement
Dim key As String
Dim url As String
Dim retrievedDate As Date
url = "http://www.fechadehoy.com/venezuela"
key = "Fecha actual: "

''''''''''Bonus: Use an HTTP request to get the date instead of opening IE'''''''''''
With req                                                                            '
    .Open "GET", url, False                                                         '
    .send                                                                           '
    doc.body.innerHTML = .responseText                                              '
    'Debug.Print .responseText                                                       '
End With                                                                            '
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

For Each el In doc.getElementsByTagName("p")
    If el.innerText Like "Fecha actual*" Then
        retrievedDate = Mid(el.innerText, InStr(el.innerText, key) + Len(key), Len(el.innerText))
    End If
Next el

End Sub

You will need to add a reference to Microsoft HTML Object Libraryand Microsoft WinHTTP Services version 5.1. To do that, go to VB editor>Tools>References.

Having the date in this format, means it can easily be manipulated. An example would be the use of functions like day(retrievedDate) , month(retrievedDate), year(retrievedDate) etc.

Answer:

if you need Lunes, 19 de agosto de 2019 then use getElementById for fecha

enter image description here

Debug.Print IE.document.getElementById("fecha").Innerhtml

enter image description here

Answer:

Why go for IE when xhr can do the trick? You can get the date with the blink of an eye if you opt for XMLHttpRequest.

Sub GetCurrentDate()
    Dim S$

    With New XMLHTTP
        .Open "GET", "http://www.fechadehoy.com/venezuela", False
        .send
        S = .responseText
    End With

    With New HTMLDocument
        .body.innerHTML = S
        MsgBox .getElementById("fecha").innerText
    End With
End Sub

Reference to add:

Microsoft XML, v6.0
Microsoft HTML Object Library

enter image description here

To get rid of that reference altogether:

Sub GetCurrentDate()
    Dim S$

    With CreateObject("MSXML2.XMLHTTP")
        .Open "GET", "http://www.fechadehoy.com/venezuela", False
        .send
        S = .responseText
    End With

    With CreateObject("htmlfile")
        .body.innerHTML = S
        MsgBox .getElementById("fecha").innerText
    End With
End Sub