Odd HTML Scrape Problem

Themilkybarkid

New Member
Joined
Jan 27, 2020
Messages
2
Hi all,

I am trying to parse a table from a website. I have used similar code successfully in the past but when using it to scrape this particular website it returns some odd data. Having inspected the web page the data I want is located within the td tags as expected but I can't figure out why the VBA code isn't reading it properly.

All help and feedback welcome

Many thanks



Sub RetrieveData()

Dim myurl As String
Dim TDElement As Object
Dim TDElements As IHTMLElementCollection
Dim IE As MSXML2.XMLHTTP60
Dim HTMLDoc As MSHTML.HTMLDocument
Dim HTMLBody As MSHTML.HTMLBody
Dim k As Long
Dim j As Long
Dim l As Long
Dim sht As Worksheet


ThisWorkbook.Sheets("Sheet1").Range("B3:L300").ClearContents


Set IE = New MSXML2.XMLHTTP60
Set HTMLDoc = New MSHTML.HTMLDocument
Set HTMLBody = HTMLDoc.body

myurl = "Market Data | EPEX SPOT"
IE.Open "GET", myurl, False
IE.send

On Error Resume Next


HTMLDoc.body.innerHTML = IE.responseText
Flat = False
k = 0
j = 3

Set TDElements = HTMLDoc.getElementsByTagName("td")
For Each TDElement In TDElements
ThisWorkbook.Sheets("Sheet1").Cells(j, k + 2) = TDElement.innerText
k = k + 1
If k = 7 Then j = j + 1
If k = 7 Then k = 0
Next


End Sub
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Not sure but seemed to work just fine for me as you had it coded. What do you mean by 'not reading it properly'?
 
Upvote 0
I am looking at that at the moment. I can't confirm this is the OPs problem but check the Excel Column H figures from the bottom up compared to the actual table values.
Is it me or do some of those figures not match...
 
Upvote 0
I am looking at that at the moment. I can't confirm this is the OPs problem but check the Excel Column H figures from the bottom up compared to the actual table values.
Is it me or do some of those figures not match...
My testing showed k=27 to be the number where data lined up in proper columns
 
Upvote 0
K=9 sorted the problem I saw when trying the code.

Maybe the OP will report back....
 
Upvote 0
hi guys, thanks for all the replies. K=9 has appeared to help with the formatting.

What I can't get my head around is once you run the code it doesn't then seem to update correctly as you re-run it. It returns spurious figures that are similar but not the exact values shown in the table. It's almost as if once you run the code once, there is a block on being able to scrape the page again. As the website updates live I can't show you what I mean but if you wait 5 minutes or so between running the code a second time and look at the web page you should see discrepancies between it and what the VBA returns you.
 
Upvote 0

Forum statistics

Threads
1,223,099
Messages
6,170,114
Members
452,302
Latest member
TaMere

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top