{"data":{"id":71,"slug":"xpath-for-nested-structures-part-4-71","title":"XPath for nested structures, part 4","excerpt":"For part 4, we revisit the basics with a fresh example and a few lessons learned the hard way. When the data you want is several levels deep and the markup has few useful attributes, XPath's ability…","seriesName":"XPath for nested structures","seriesPart":4,"sections":[{"heading":"Introduction","paragraphs":["For part 4, we revisit the basics with a fresh example and a few lessons learned the hard way. When the data you want is several levels deep and the markup has few useful attributes, XPath's ability to navigate up, down and sideways through the tree becomes very handy."]},{"heading":"Common pitfalls","paragraphs":["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.","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.","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":"A practical approach","paragraphs":["Validate the data you extract. Prices without currencies, dates without time zones and empty fields are common sources of silent errors downstream.","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."]},{"heading":"Key takeaway","paragraphs":["Anchor on a stable nearby element, then use relative paths and axes such as following-sibling and ancestor rather than absolute paths from the root."]}],"authorId":11,"authorName":"Oliver Kim","authorSlug":"oliver-kim","category":"Scraping Basics","tags":["ethics","scrapy","testing"],"publishedAt":"2025-06-12T00:00:00.000Z","wordCount":215,"readingTimeMinutes":2}}