How to scrape data from sites that need a login
A lot of useful data sits behind a login. Sports stats platforms, supplier portals and analytics dashboards all ask you to sign in before they show anything. Collecting from them is harder than scraping a public page, but it can be done reliably.
On our sports data platform, we collect data from 11 sources, including PFF, Wyscout, NCAA and ESPN, across 54 leagues. Some of them need a login, and the approach below is the one we use for those.
Before you start, read the source’s terms of use, and only collect data you have the right to use.
Don’t scrape the page, call the API behind it
Most modern websites load their data from their own API endpoints, then draw it on the page with JavaScript. Open your browser’s developer tools, go to the Network tab, and load the page. You will usually see requests that return clean JSON with the exact data shown on screen.
Calling those endpoints directly has big advantages over scraping the HTML:
- The data comes back structured, so you don’t have to dig it out of the page.
- Requests are smaller and faster than loading a full page in a browser.
- A redesign of the page doesn’t break your scraper, because the API usually stays the same.
Use a real browser only for the login
The hard part is that those endpoints expect a logged-in user. Logins often involve redirects, JavaScript and tokens that are painful to copy by hand.
So we use Playwright, a browser automation tool, for that one step. Playwright opens a real browser, signs in like a person would, and then we capture the session cookies and tokens it receives. After that, we make the API calls ourselves with those credentials, without the browser.
This keeps the slow, fragile part (the browser) to a single step, and the fast part (the API calls) does all the volume.
Clean the data on arrival
API responses usually carry much more than you need: internal IDs, display settings, tracking fields. We strip the extra metadata and unused fields as soon as the data arrives, before anything is stored. Your database stays small and your exports stay readable.
Expect things to fail
Sessions expire, requests time out, and sites change their endpoints. A scraper that runs on a schedule has to cope with all of that while nobody is watching it:
- Retry failed requests, with a short wait between attempts
- Log in again when a session expires
- Log every run, so you can see what happened
- Send an alert, such as a Slack message, when a run still fails
Deliver the data where it’s used
The last step is getting the data to the people who use it. On our sports platform, each scheduled run ranks the players by the client’s weighted stats, writes a CSV and uploads it to AWS S3.
Read the full sports data platform case study, or see what else we build under data scraping and pipelines. If you need data from sources like these, tell us about it.