Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

Importing web data

0

I've looked at various methods to import data into an Excel woorkbbok but have been encountering problems.

I've tried using Data / From Web but see amessage stating my browser doesn't support the required functions, default browser is Edge but I have Chrome and Firefox installed.

I watched TeachExcel's excellent video "Import Web Data to Excel using VBA Macros" and thought that would work but the HTML appears to differ and not sure what or how I should be capturing from the page for it to work.

The page in question is flashscore.co.uk

Ideally I'd like to capture the first 20 Football leagues with matches listed.

If someone could point me in the right direction I'd be very grateful.

Many thanks

Tony

Answer
Discuss

Answers

0
Selected Answer

Tony

I couldn't get fixtures from that site but I modified the tutorial code to get the first game today from the BBC football website using this code (modifed website appears in the first bold line and the details between the next two bold lines have been tweaked)::

Sub Get_Web_Data()
' TeachExcel.com

Dim request As Object
Dim response As String
Dim html As New HTMLDocument
Dim website As String
Dim price As Variant

' Website to go to.
website = "https://www.bbc.co.uk/sport/football/scores-fixtures"

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

' Where to go and how to go there - probably don't need to change this.
request.Open "GET", website, False

' Get fresh data.
request.setRequestHeader "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT"

' Send the request for the webpage.
request.send

' Get the webpage response data into a variable.
response = StrConv(request.responseBody, vbUnicode)

' Put the webpage into an html object to make data references easier.
html.body.innerHTML = response

' Get the teams from the specified element on the page.
Home = html.getElementsByClassName("gs-u-display-none gs-u-display-block@m qa-full-team-name sp-c-fixture__team-name-trunc").Item(0).innerText
Away = html.getElementsByClassName("gs-u-display-none gs-u-display-block@m qa-full-team-name sp-c-fixture__team-name-trunc").Item(1).innerText
' Output the fixture in a message box.
Fixtures = Date & " Fixtures:" & Chr(13) & Home & " v " & Away

MsgBox Fixtures

End Sub

Try that (today) and you should get the message:

19/01/2021 Fixtures:

West Ham United v West Bromwich Albion

If you change the bold 0 and 1 (in lines starting Home and Away) to 2 and 3, today you'll get:

19/01/2021 Fixtures:

Leicester City v Chelsea

I don't know how yet how  to loop through all the similar classes but you have a starter hopefully.

p.s. League 2 is my home!

Discuss

Discussion

Many thanks for your quick response and solution.

Unfortunately the source has to be flashscores as it's the only site that allows me to select favourite leagues which is an important element of the task in hand.

I have dropdowns and IFS formula's to do what I want and was hoping to save some time in match selection.

Still not all bad at least I found this place.

Many thanks again for your speedy assistance.

Tony 
TonyF (rep: 2) Jan 19, '21 at 12:04 pm
Tony. Suggest you deselect my answer (if that's possible) so others  might try to answer. Could be however that flashscore.co.uk has anti-scraping provisions in place.
John_Ru (rep: 6142) Jan 19, '21 at 12:35 pm
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login