{"data":{"id":140,"slug":"when-to-use-an-api-instead-of-scraping-part-7-140","title":"When to use an API instead of scraping, part 7","excerpt":"For part 7, we revisit the basics with a fresh example and a few lessons learned the hard way. If a site offers an API for the data you need, scraping its HTML is usually the harder, more fragile and…","seriesName":"When to use an API instead of scraping","seriesPart":7,"sections":[{"heading":"Introduction","paragraphs":["For part 7, we revisit the basics with a fresh example and a few lessons learned the hard way. If a site offers an API for the data you need, scraping its HTML is usually the harder, more fragile and less polite option."]},{"heading":"Step by step","paragraphs":["Read the site's terms of service before you start. Many sites welcome automated access within limits and say so explicitly; others ask you to use an API instead.","Store raw responses alongside parsed output while you are developing. When a parser breaks, you can replay the saved HTML instead of hitting the site again.","Dynamic pages render content after the initial HTML arrives. A browser automation tool can wait for a specific element to appear, which is far more reliable than sleeping for a fixed amount of time."]},{"heading":"Common pitfalls","paragraphs":["Treat HTTP 429 responses as a signal, not an obstacle. Read the Retry-After header, back off, and resume at a slower pace.","Deduplicate as you go. The same item often appears on several listing pages, and a stable identifier such as a SKU or ID makes duplicates easy to spot.","Keep configuration such as base URLs, delays and selectors out of your code. Changing a delay should not require a redeploy."]},{"heading":"Key takeaway","paragraphs":["Check for a documented API, a data export or a feed first. Scrape only when none exists, and read the terms of service either way."]}],"authorId":8,"authorName":"Hannah Novak","authorSlug":"hannah-novak","category":"Ethics & Law","tags":["scrapy","python","javascript","http"],"publishedAt":"2024-07-30T00:00:00.000Z","wordCount":227,"readingTimeMinutes":2}}