Disclaimers: IANAL. And I run https://serpapi.com. I can give you free credits to your students for ML uses if you want.
Legality highly depends on where you are.
In the US, scraping of public data is a fair use exception protected by the first. If you have to sign in to access the data, you then might be bound by the ToS.
In Europe, scraping of public data can be against several laws. Notably GDPR, the new copyright law, and you might be infringing copyrights on database as defined by the CNIL.