<?xml version="1.0" encoding="UTF-8"?>
<response><data><post><id>200</id><slug>when-to-use-an-api-instead-of-scraping-part-10-200</slug><title>When to use an API instead of scraping, part 10</title><excerpt>Part 10 looks at what changes when the same technique has to run every day in production. If a site offers an API for the data you need, scraping its HTML is usually the harder, more fragile and less…</excerpt><seriesName>When to use an API instead of scraping</seriesName><seriesPart>10</seriesPart><sections><item><heading>Introduction</heading><paragraphs><item>Part 10 looks at what changes when the same technique has to run every day in production. 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>What to watch for</heading><paragraphs><item>Always identify your client honestly with a descriptive User-Agent, and keep your request rate modest. A crawler that behaves like a considerate visitor is far less likely to cause problems for the site operator.</item><item>Pagination comes in several shapes: numbered pages, next links, offsets and opaque cursors. Each has trade-offs, and a robust scraper should detect the end of the result set rather than assuming a fixed number of pages.</item><item>Log every request with its URL, status code and duration. When something goes wrong at two in the morning, those logs are the difference between a quick fix and a long investigation.</item></paragraphs></item><item><heading>Going further</heading><paragraphs><item>Validate the data you extract. Prices without currencies, dates without time zones and empty fields are common sources of silent errors downstream.</item><item>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.</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>4</authorId><authorName>Lucas Silva</authorName><authorSlug>lucas-silva</authorSlug><category>Ethics &amp; Law</category><tags><item>javascript</item><item>playwright</item><item>python</item><item>scrapy</item></tags><publishedAt>2024-10-26T00:00:00.000Z</publishedAt><wordCount>246</wordCount><readingTimeMinutes>2</readingTimeMinutes></post></data></response>
