<?xml version="1.0" encoding="UTF-8"?>
<response><data><post><id>180</id><slug>when-to-use-an-api-instead-of-scraping-part-9-180</slug><title>When to use an API instead of scraping, part 9</title><excerpt>In part 9 we take the ideas from earlier instalments and apply them to a larger, messier site. If a site offers an API for the data you need, scraping its HTML is usually the harder, more fragile and…</excerpt><seriesName>When to use an API instead of scraping</seriesName><seriesPart>9</seriesPart><sections><item><heading>Introduction</heading><paragraphs><item>In part 9 we take the ideas from earlier instalments and apply them to a larger, messier site. If a site offers an API for the data you need, scraping its HTML is usually the harder, more fragile and less polite option.</item></paragraphs></item><item><heading>Common pitfalls</heading><paragraphs><item>Character encodings still cause trouble. Decode responses using the charset the server declares, and normalise text before comparing or storing it.</item><item>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.</item><item>Write small, focused tests for your parsers using saved HTML fixtures. They run in milliseconds and catch layout changes long before a full crawl would.</item></paragraphs></item><item><heading>What to watch for</heading><paragraphs><item>Be careful with relative URLs. Resolve every link against the page it came from, not against the site root, or you will request pages that do not exist.</item><item>Selectors are the most fragile part of any scraper. Prefer stable attributes such as data-testid or semantic structure over generated class names, which can change on every deploy.</item></paragraphs></item><item><heading>Key takeaway</heading><paragraphs><item>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.</item></paragraphs></item></sections><authorId>9</authorId><authorName>Mateo Hughes</authorName><authorSlug>mateo-hughes</authorSlug><category>Ethics &amp; Law</category><tags><item>http</item><item>ethics</item></tags><publishedAt>2024-08-11T00:00:00.000Z</publishedAt><wordCount>195</wordCount><readingTimeMinutes>2</readingTimeMinutes></post></data></response>
