mardi 24 septembre 2019

vba - get web page data through class nam

I need to get the dates and temp from a weather website and record it on cells but I am getting a object variable or with block variable not set error.

I tried to data from web in excel but I think the website is protected or something because I keep getting "under maintenance" page when trying to load the page from excel. I got the codes below from a tutorial but I can't make it work.

Sub record()

Dim request As Object
Dim response As String
Dim html As New HTMLDocument
Dim websie As String
Dim temps As Variant

'provide link
'website = "https://finance.yahoo.com/quote/EURUSD=X?p=EURUSD=X"
website = "https://www.accuweather.com/en/us/chicago/60608/september-weather/348308"

'create the object that will make the webpage request
Set request = CreateObject("MSXML2.XMLHTTP")

'go to the link
request.Open "GET", website, False

'send request for webpage
request.send

'get web response data to variable
response = StrConv(request.responseBody, vbUnicode)

'put webpage to an html object
html.body.innerHTML = response

'get temperature from specified element
'temps = html.getElementsByClassName("Trsdu(0.3s) Fw(b) Fz(36px) Mb(-4px) D(ib)")(0).innerText
temps = html.getElementsByClassName("high")(0).innerText


 Sheets("record").Range("A1") = temps

End Sub

Sample lines from the website:

```<a class="monthly-daypanel is-past">
   <div class="date">2</div>
   <div class="icon-container"...</div>
   <div class="temp">
      <div class="high">83</div>
      <div class="low">83</div>
   </div>
</a>```

I want to get the date, high and low.




Aucun commentaire:

Enregistrer un commentaire