-
Notifications
You must be signed in to change notification settings - Fork 0
/
redfinPropertyScraper.js
58 lines (50 loc) · 2.23 KB
/
redfinPropertyScraper.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
require('dotenv').config();
const axios = require('axios');
const cheerio = require('cheerio');
const API_URL = 'https://api.scraperapi.com';
const redfinPropertyScraper = async (API_KEY, REDFIN_PAGE_URL) => {
console.log('Fetching data with ScraperAPI...');
const queryParams = new URLSearchParams({
api_key: API_KEY,
url: REDFIN_PAGE_URL,
country_code: 'us'
});
try {
const response = await axios.get(`${API_URL}?${queryParams.toString()}`);
const html = response.data;
const $ = cheerio.load(html);
const propertyList = [];
console.log('Extracting information from the HTML...');
$(".HomeCardContainer").each((_, el) => {
const price = $(el).find('.bp-Homecard__Price--value.span') > 0 ?
$(el).find('.bp-Homecard__Price--value.span').text() :
$(el).find('.bp-Homecard__Price--value').text();
const beds = $(el).find('.bp-Homecard__Stats--beds.text-nowrap').text();
const bath = $(el).find('.bp-Homecard__Stats--baths.text-nowrap').text();
const space = $(el).find('.bp-Homecard__LockedStat--value').text();
const address = $(el).find('bp-Homecard__Address--address') > 0 ?
$(el).find('.bp-Homecard__Address--address').text() :
$(el).find('.bp-Homecard__Address').text();
const link = $(el).find('.link-and-anchor.visuallyHidden').attr('href');
const linkText = $(el).find('.link-and-anchor.visuallyHidden').text();
const contact = $(el).find('.RentalCTAContact__button--phone .ButtonLabel').text();
if (!price) {
return
}
propertyList.push({
price,
address: address ?? linkText,
beds,
bath,
space,
link: link ? `https://www.redfin.com${link}` : null,
contact
});
});
console.log('JSON result:', propertyList);
} catch (error) {
console.log(error)
}
};
module.exports = redfinPropertyScraper;
// Reference: https://hasdata.com/blog/how-to-scrape-redfin-data, https://www.scraperapi.com/blog/scrape-redfin/