{"data":{"id":68,"slug":"working-with-csrf-tokens-part-4-68","title":"Working with CSRF tokens, part 4","excerpt":"This is part 4 of our series, and this time we focus on edge cases readers asked about. Cross-site request forgery tokens are one of the first things that trip up a script trying to log in to a site:…","seriesName":"Working with CSRF tokens","seriesPart":4,"sections":[{"heading":"Introduction","paragraphs":["This is part 4 of our series, and this time we focus on edge cases readers asked about. Cross-site request forgery tokens are one of the first things that trip up a script trying to log in to a site: the form posts fine in a browser but fails with a 403 from code."]},{"heading":"A practical approach","paragraphs":["Time zones matter more than they seem. Store timestamps in UTC and convert only when displaying them.","Treat HTTP 429 responses as a signal, not an obstacle. Read the Retry-After header, back off, and resume at a slower pace.","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."]},{"heading":"Common pitfalls","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.","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":"Key takeaway","paragraphs":["Load the form page with a session that keeps cookies, read the hidden token field, and send it back with the same session in the same request."]}],"authorId":3,"authorName":"Priya Nakamura","authorSlug":"priya-nakamura","category":"Testing","tags":["python","http","scrapy","ethics"],"publishedAt":"2025-01-21T00:00:00.000Z","wordCount":208,"readingTimeMinutes":2}}