Data & research

How do I scrape data from a website into a spreadsheet?

TODO for AI · updated

For one simple table, Google Sheets =IMPORTHTML(url, "table", 1) or Excel's Data, From Web works. For lists across many pages, behind a login or loaded by JavaScript, you need a browser that clicks through: an extension, a script, or an agent working in your own logged-in browser. Check the site's terms before you collect data.

Do this for me Free plan, no card. It asks before sending or posting anything.

How to do it

  1. One table. Google Sheets: =IMPORTHTML("url", "table", 1). Excel: Data, From Web, pick the table.
  2. Specific elements. Google Sheets =IMPORTXML(url, xpath) pulls items by XPath, for example every product name.
  3. Many pages or a login. Formulas cannot log in or click Next. Use a browser-based tool that follows pagination.
  4. Clean it. Remove duplicates, split combined fields, convert prices and dates to numbers.
  5. Respect the site. Read the terms and robots.txt, go slowly, and do not collect personal data you have no reason to hold.

Your options

OptionEffortCatch
IMPORTHTML / Power QueryOne formula or queryNo logins or click-through pagination
Scraper extensionsBuild a selector recipeBreaks when the page changes
Python scriptWrite and maintain codeNeeds a developer
TODO for AIDescribe what you wantSlower than a script on huge sites

Watch the agent do it

A real run on sample files. Yours starts from here.

Get every book from https://books.toscrape.com (all pages) into a spreadsheet with title, price, star rating, stock status and link. Then tell me the 10 cheapest 5 star books.
AI
shellCreate scrape folder and verify Python libraries
shellInstall and verify Python scraping libraries
writescrape.py
shellScrape books and summarize spreadsheet
shellShow last lines of books spreadsheet
filebooks.xlsx

I scraped all 50 pages of books.toscrape.com, which gave 1,000 books. They're in ~/work/scrape/books.xlsx on the cloud computer, with columns for title, price (£), star rating (1 to 5), stock status and a link to each book. The file is also shown above. The scraper that made it is scrape.py in the same folder.

Every one of the 1,000 books is listed as "In stock", so the stock column doesn't tell any books apart.

The 10 cheapest 5-star books (196 books have 5 stars):

# Price Title
1 £10.00 An Abundance of Katherines
2 £10.23 Greek Mythic History
3 £11.05 The Power Greens Cookbook: 140 Delicious Superfood Recipes
4 £11.21 Dear Mr. Knightley
5 £11.33 The Darkest Corners
6 £11.38 Naturally Lean: 125 Nourishing Gluten-Free, Plant-Based Recipes--All Under 300 Calories
7 £11.64 Fruits Basket, Vol. 2 (Fruits Basket #2)
8 £11.83 Old School (Diary of a Wimpy Kid #10)
9 £11.89 Superman Vol. 1: Before Truth (Superman by Gene Luen Yang #1)
10 £12.16 Every Heart a Doorway (Every Heart A Doorway #1)
Now yours. Pick a case or write your own:
Runs as your own TODO. Attach your files on the next screen.

FAQ

Is web scraping legal?
It depends on the site's terms, the data and your country. Public, non-personal data used responsibly is the safe case. When in doubt, ask the site or use its export or API.
Can it scrape pages behind a login?
Yes, in your own logged-in browser, for data your account can see anyway.
Can it keep the sheet up to date?
Yes. Schedule it daily or weekly and it adds new rows and marks changed ones.
What about sites that load data with JavaScript?
The agent uses a real browser, so it sees the page the way you do.

Related