Published on

A Guide to Web Scraping with Cookie Management

Authors

Introduction

If you are a sports fan, then you know the feeling of anticipation that comes with waiting for tickets to go on sale for your favorite team's matches. But with high demand and limited availability, obtaining those coveted tickets can feel like a race against time.

While trying to purchase tickets for my favorite team matches, I encountered a familiar issue: how to know when tickets are available without having to keep checking the ticketing website constantly? This led me down the path of web scraping, a technique that allows automated data extraction from websites. However, I quickly discovered that my efforts were being blocked by server restrictions, resulting in HTTP status code 401 Unauthorized (or 403 Forbidden).

Well, this happened because many websites employe security measures to prevent automated access, such as detecting requests from bots. To bypass these restrictions, I needed to simulate human-like behavior, which included handling cookies.

Understanding Cookies

When you visit a website, it often sends cookies to your browser, which are small pieces of data stored on your computer. These cookies contain information such as session IDs, authentication tokens, and user preferences. When you make subsequent request to the website, your browser automatically sends back these cookies to the server, allowing it to identify you and maintain your session.

Implementation in JavaScript

To bypass the server restrictions and effectively extract data from the ticketing website, cookie management became essential within our web scraping script.

Prerequisites

The implementation relies on three key libraries:

  • axios: An HTTP client for making requests to web servers.
  • tough-cookie: A library for handling HTTP cookies, which provides a flexible and powerful API for managing cookies.
  • axios-cookiejar-support: An Axios adapter that enables cookie jar support, allowing you to store and manage cookies across requests.

Initiating cookie management involves two fundamental steps:

  • Creating a CookieJar instance: We instantiate a new CookieJar object from the tough-cookie library. This object serves as a container and stores cookies that are sent and received during requests.

  • Creating an Axios Client: We wrap our Axios instance with the cookie jar support using the wrapper function provided by axios-cookiejar-support. This enables our client to automatically handle cookies across requests.

const { wrapper } = require('axios-cookiejar-support')
const toughCookie = require('tough-cookie')

const jar = new toughCookie.CookieJar()
const client = wrapper(axios.create({ jar }))

Having set up our client, we can now make requests to websites that require cookies. We also include the withCredentials option in the request configuration to ensure that cookies are included in the request. Finally, it's important to set a User Agent in the headers to simulate a real browser.

const response = await client.get(url, {
  withCredentials: true, // Include cookies in the request
  headers: {
    // Set a user agent to simulate a real browser
    'User-Agent':
      'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36',
  },
})

Now we can extract the data from the response and continue with our web scraping process. Extracting data is outside the scope of this guide, but you can use libraries like Cheerio or Puppeteer to parse the HTML and extract the information you need.

Conclusion

By including cookies in our web scraping script, we can effectively bypass server restrictions and extract data from websites that require authentication or session management. Cookie management is a crucial aspect of web scraping, enabling us to simulate human-like behavior and access data that would otherwise be inaccessible.