{"data":{"id":37,"slug":"reading-sitemaps-to-discover-urls-part-2-37","title":"Reading sitemaps to discover URLs, part 2","excerpt":"For part 2, we revisit the basics with a fresh example and a few lessons learned the hard way. Before you crawl a site link by link, check whether it already publishes a list of every page it wants…","seriesName":"Reading sitemaps to discover URLs","seriesPart":2,"sections":[{"heading":"Introduction","paragraphs":["For part 2, we revisit the basics with a fresh example and a few lessons learned the hard way. Before you crawl a site link by link, check whether it already publishes a list of every page it wants found. That list is usually called sitemap.xml."]},{"heading":"Going further","paragraphs":["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.","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.","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."]},{"heading":"A practical approach","paragraphs":["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.","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."]},{"heading":"Key takeaway","paragraphs":["Look for the Sitemap line in robots.txt, follow sitemap index files to their children, and use lastmod to skip pages that have not changed."]}],"authorId":2,"authorName":"Noah Bennett","authorSlug":"noah-bennett","category":"Scraping Basics","tags":["http","javascript"],"publishedAt":"2024-05-08T00:00:00.000Z","wordCount":228,"readingTimeMinutes":2}}