Skip to content

Latest commit

 

History

History
113 lines (88 loc) · 2.87 KB

File metadata and controls

113 lines (88 loc) · 2.87 KB

Redfin Scraper — JavaScript (apify-client) examples

Call the hosted Redfin Scraper from Node.js with the official apify-client.

Install

npm install apify-client

Basic run

import { ApifyClient } from 'apify-client';

const client = new ApifyClient({ token: 'YOUR_APIFY_TOKEN' });

const run = await client.actor('logiover/redfin-scraper').call({
  searchUrls: ['https://www.redfin.com/city/30818/TX/Austin'],
  listingType: 'forSale',
  sortBy: 'newest',
  minPrice: 300000,
  maxPrice: 900000,
  minBeds: 3,
  maxResults: 500,
});

const { items } = await client.dataset(run.defaultDatasetId).listItems();
console.log(`Scraped ${items.length} homes`);
console.log(items[0]);

Build sold comps for a ZIP and save to a file

import { ApifyClient } from 'apify-client';
import { writeFileSync } from 'node:fs';

const client = new ApifyClient({ token: 'YOUR_APIFY_TOKEN' });

const run = await client.actor('logiover/redfin-scraper').call({
  searchUrls: ['90210'],
  listingType: 'sold',
  sortBy: 'recommended',
  maxResults: 500,
});

const { items } = await client.dataset(run.defaultDatasetId).listItems();

const comps = items.map((h) => ({
  address: h.address,
  price: h.price,
  soldDate: h.soldDate,
  beds: h.beds,
  baths: h.baths,
  sqFt: h.sqFt,
  pricePerSqFt: h.pricePerSqFt,
  daysOnMarket: h.daysOnMarket,
  mls: h.mlsId,
}));

writeFileSync('comps-90210.json', JSON.stringify(comps, null, 2));
console.log(`Saved ${comps.length} sold comps`);

Scan several ZIPs and merge results

import { ApifyClient } from 'apify-client';

const client = new ApifyClient({ token: 'YOUR_APIFY_TOKEN' });

const run = await client.actor('logiover/redfin-scraper').call({
  searchUrls: ['78704', '78745', '78702'],
  listingType: 'forSale',
  minBeds: 3,
  maxResults: 1000,
});

const { items } = await client.dataset(run.defaultDatasetId).listItems();
const byZip = {};
for (const home of items) {
  (byZip[home.zip] ??= []).push(home);
}
for (const [zip, homes] of Object.entries(byZip)) {
  const avg = Math.round(homes.reduce((s, h) => s + (h.pricePerSqFt || 0), 0) / homes.length);
  console.log(`${zip}: ${homes.length} homes, avg $${avg}/sqft`);
}

Stream a large dataset page by page

import { ApifyClient } from 'apify-client';

const client = new ApifyClient({ token: 'YOUR_APIFY_TOKEN' });

const run = await client.actor('logiover/redfin-scraper').call({
  metro: 'phoenix-az',
  listingType: 'forSale',
  maxResults: 3000,
});

const dataset = client.dataset(run.defaultDatasetId);
let offset = 0;
const limit = 500;
while (true) {
  const { items } = await dataset.listItems({ offset, limit });
  if (items.length === 0) break;
  for (const home of items) console.log(home.address, home.price);
  offset += items.length;
}