Automate Apartments Search with Puppeteer

July 29, 2022

4 min read

Automate Apartments Search with Puppeteer
Watch on YouTube

I'm looking for a new apartment, but I don't want to spend too much time checking the marketplace website. So let's write a TypeScript program that will scrap all the apartments we haven't seen and show them in Chrome browser.

For now, I've implemented the search only for one marketplace called But we can also expand it to other websites by implementing the showNewRealEstate function for them.

import puppeteer, { Page } from "puppeteer"

import { Storage } from "./Storage"

const msInDay = 86400000
const nextPageClassName = "step-forward"

const realEstateSearchPage = ``

const getByClassName = (element: Page, className: string) => {
  return element.$$(`.${className}`)

const wait = (ms = 1000) => new Promise((r) => setTimeout(r, ms))

interface Unit {
  url: string
  id: string
  date: number
  ad: boolean

const getRealEstate = (): Unit[] => {
  const REAL_ESTATE_CLASS_NAME = "statement-card"
  const PAGINATION_CLASS_NAME = "pagination-container"
  const CARD_RIGHT_INFO_CLASS_NAME = "d-block"
  const AD_LABEL = "vip-label"

  const [pagination] = document.getElementsByClassName(PAGINATION_CLASS_NAME)

  const cards = [
  ].filter((card) => !card.className.includes("banner"))

  const year = new Date().getFullYear()

  return => {
    const [rawId, rawDate] = [
    ].map((info) => (info as HTMLElement).innerText)
    const [day, monthString, time] = rawDate.split(" ")
    const rawDateWithYear = [day, monthString, year, time].join(" ")
    const date = new Date(rawDateWithYear).getTime()
    const id = rawId.split(" ")[1]
    return {
      url: (card.children[0] as HTMLAnchorElement).href,
      ad: card.getElementsByClassName(AD_LABEL).length > 0,

export const showNewRealEstate = async () => {
  const browser = await puppeteer.launch({
    headless: false,
    defaultViewport: null,
    args: ["--start-fullscreen"],
  let realEstatesPage = await browser.newPage()
  await realEstatesPage.goto(realEstateSearchPage)

  const storage = new Storage("myhome-ge")
  const lastVisitAt = storage.state.lastVisitAt || - msInDay * 2

  let units: Unit[] = []
  const recursiveSearch = async (): Promise<void> => {
    const newUnits = (await realEstatesPage.evaluate(getRealEstate)).filter(
      (unit) => > lastVisitAt
    if (newUnits.length < 1) return


    const [nextPage] = await getByClassName(realEstatesPage, nextPageClassName)
    await wait()

    await realEstatesPage.reload({ waitUntil: "domcontentloaded" })
    await wait()

    return recursiveSearch()
  await recursiveSearch()

  const newUnits = units.filter((a) => !storage.state.shown.includes(

  for (const { url } of newUnits) {
    const page = await browser.newPage()
    await page.goto(url)

  storage.state = {
    shown: [, =>],
  console.log( => a.url))

First, we launch the Chrome browser with Puppeteer and go to the website. Then we want to initialize the storage that keeps the state in a local JSON file. We only store a timestamp of the last visit so that we don't check for apartments posted before that date and the list of apartment ids that our script already showed us. We read the state from the file in the constructor and leverage getters and setters to update the file on state change. It's a simple approach for a small script - no need for rocket science here.

export interface State {
  shown: string[]
  lastVisitAt?: number

export const defaultState: State = {
  shown: [],
import fs from "fs"
import { defaultState, State } from "./State"

export class Storage {
  readonly filePath: string
  private _state: State = defaultState

  constructor(name: string) {
    this.filePath = `./state/${name}.json`

    if (!fs.existsSync(this.filePath)) {
      this._state = defaultState
    } else {
      this._state = JSON.parse(fs.readFileSync(this.filePath).toString())

  get state(): State {
    return this._state

  set state(newState: State) {
    fs.writeFileSync(this.filePath, JSON.stringify(newState))

    this._state = newState

We use a recursive function to get the list of apartments posted after our last visit. First, we convert HTML cards into an array of units with the getRealEstate function. Then we filter them according to date, and if the list is empty, we exit the recursion. Otherwise, we click the "Next" button, reload the page, and continue the recursion.

Once we have the list of apartments, we filter them to skip units we've seen before and show them in the browser. Finally, we update our state with the new visit date and an array of apartment ids.

Now let's run the script. Here we can see how it adds new tabs with apartments and then update the JSON file with a new state.