{"data":{"id":12,"slug":"session-cookies-for-scrapers-12","title":"Session cookies for scrapers","excerpt":"Logging in once and staying logged in is a matter of keeping the cookies the server gives you and sending them back on every later request.","seriesName":"Session cookies for scrapers","seriesPart":1,"sections":[{"heading":"Introduction","paragraphs":["Logging in once and staying logged in is a matter of keeping the cookies the server gives you and sending them back on every later request."]},{"heading":"What to watch for","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.","Validate the data you extract. Prices without currencies, dates without time zones and empty fields are common sources of silent errors downstream.","Character encodings still cause trouble. Decode responses using the charset the server declares, and normalise text before comparing or storing it."]},{"heading":"Step by step","paragraphs":["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.","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.","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."]},{"heading":"Key takeaway","paragraphs":["Use a session object (requests.Session, a Playwright context) for the whole run, and check for redirects to the login page as a sign the session has expired."]}],"authorId":1,"authorName":"Amelia Okafor","authorSlug":"amelia-okafor","category":"Scraping Basics","tags":["http"],"publishedAt":"2025-06-14T00:00:00.000Z","wordCount":222,"readingTimeMinutes":2}}