Repository navigation
Expand file tree
/
Copy pathextract_postcode.js
More file actions
255 lines (226 loc) · 8.84 KB
/
Copy pathextract_postcode.js
File metadata and controls
255 lines (226 loc) · 8.84 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/**
* Finn.no Property Data Extractor & CSV Generator
*
* This script extracts property information from a Finn.no listing page and
* generates a CSV file matching property_listings_latest.csv format.
*
* Usage:
* 1. Open a Finn.no property listing in your browser
* 2. Open Developer Tools (F12 or Cmd+Option+I on Mac)
* 3. Go to the Console tab
* 4. Paste this entire script and press Enter
* 5. A CSV file will be automatically downloaded
*
* CSV Format: title,address,price,size,link
* Files are saved to: enhanced_listings/ folder (configure in your browser download settings)
*/
(function extractPropertyData() {
const pageText = document.body.innerText;
const currentUrl = window.location.href;
// Extract Finn.no listing ID from URL
const finnIdMatch = currentUrl.match(/finnkode=(\d+)/) || currentUrl.match(/\/(\d+)$/);
const finnId = finnIdMatch ? finnIdMatch[1] : Date.now().toString();
// Helper function to escape CSV values
function escapeCSV(value) {
if (!value) return '';
// Convert to string and escape quotes
const str = String(value).replace(/"/g, '""');
// Wrap in quotes if contains comma, newline, or quote
if (str.includes(',') || str.includes('\n') || str.includes('"')) {
return `"${str}"`;
}
return str;
}
// Helper function to extract text from selectors
function extractText(selectors, pattern = null) {
for (const selector of selectors) {
const elem = document.querySelector(selector);
if (elem) {
const text = elem.textContent.trim();
if (text) {
if (pattern) {
const match = text.match(pattern);
return match ? match[1] : text;
}
return text;
}
}
}
return null;
}
// ============================================
// EXTRACT PROPERTY INFORMATION
// ============================================
let propertyData = {
title: 'Unknown',
address: 'Unknown',
postcode: null,
fullAddress: 'Unknown',
price: 'Unknown',
size: 'Unknown',
link: currentUrl
};
// 1. Extract Title
const titleSelectors = [
'h1',
'[data-testid="ad-title"]',
'.ad-title',
'h1[class*="title"]',
'h1[class*="heading"]'
];
propertyData.title = extractText(titleSelectors) || 'Unknown';
// 2. Extract Postcode and Address
// Try "Kart, XXXX City" pattern
const kartMatch = pageText.match(/Kart,?\s*(\d{4})\s+([A-Za-zÆØÅæøå\s]+)/i);
if (kartMatch) {
propertyData.postcode = kartMatch[1];
propertyData.fullAddress = kartMatch[1] + ' ' + kartMatch[2].trim();
}
// Try to find street address before postcode
// Look for patterns like "Streetname, XXXX City" or "Streetname XXXX City"
const addressPatterns = [
/([A-ZÆØÅ][A-Za-zÆØÅæøå\s\d]+),?\s*(\d{4})\s+([A-Za-zÆØÅæøå\s]+)/i,
/([A-ZÆØÅ][A-Za-zÆØÅæøå\s\d]+\s+\d+[A-Z]?),?\s*(\d{4})\s+([A-Za-zÆØÅæøå\s]+)/i
];
for (const pattern of addressPatterns) {
const match = pageText.match(pattern);
if (match) {
propertyData.postcode = match[2] || match[1];
if (match[1] && match[3]) {
propertyData.fullAddress = `${match[1].trim()}, ${match[2] || match[1]} ${match[3].trim()}`;
}
break;
}
}
// If no postcode found yet, try generic postcode pattern
if (!propertyData.postcode) {
const postcodeMatch = pageText.match(/\b(\d{4})\s+(Oslo|Bergen|Trondheim|Stavanger|Drammen|[A-Za-zÆØÅæøå]+)\b/i);
if (postcodeMatch) {
propertyData.postcode = postcodeMatch[1];
propertyData.fullAddress = postcodeMatch[0];
}
}
// Try to get address from meta tags or structured data
const metaAddress = document.querySelector('meta[property="og:street-address"]');
const metaLocality = document.querySelector('meta[property="og:locality"]');
if (metaAddress && metaLocality) {
const street = metaAddress.getAttribute('content');
const city = metaLocality.getAttribute('content');
if (propertyData.postcode) {
propertyData.fullAddress = `${street}, ${propertyData.postcode} ${city}`;
} else {
propertyData.fullAddress = `${street}, ${city}`;
}
}
// Final address assignment
propertyData.address = propertyData.fullAddress !== 'Unknown' ? propertyData.fullAddress : 'Unknown';
// 3. Extract Price
const pricePattern = /(\d{1,3}(?:\s?\d{3})*)\s*kr/i;
const priceMatch = pageText.match(pricePattern);
if (priceMatch) {
propertyData.price = priceMatch[1].replace(/\s/g, ' ') + ' kr';
} else {
// Try common price selectors
const priceSelectors = [
'[data-testid="ad-price"]',
'.ad-price',
'[class*="price"]',
'[class*="Price"]'
];
const priceText = extractText(priceSelectors);
if (priceText && /kr/i.test(priceText)) {
propertyData.price = priceText.match(/[\d\s]+kr/i)?.[0] || 'Unknown';
}
}
// 4. Extract Size (m²)
const sizePattern = /(\d+)\s*m[²2]/i;
const sizeMatch = pageText.match(sizePattern);
if (sizeMatch) {
propertyData.size = sizeMatch[1] + ' m²';
} else {
// Try size selectors
const sizeSelectors = [
'[data-testid="ad-size"]',
'.ad-size',
'[class*="size"]',
'[class*="Size"]'
];
const sizeText = extractText(sizeSelectors, /(\d+\s*m[²2])/i);
if (sizeText) {
propertyData.size = sizeText;
}
}
// ============================================
// GENERATE CSV
// ============================================
// CSV Header (matching property_listings_latest.csv format)
const csvHeader = 'title,address,price,size,link\n';
// CSV Row
const csvRow = [
escapeCSV(propertyData.title),
escapeCSV(propertyData.address),
escapeCSV(propertyData.price),
escapeCSV(propertyData.size),
escapeCSV(propertyData.link)
].join(',') + '\n';
const csvContent = csvHeader + csvRow;
// ============================================
// DOWNLOAD CSV FILE
// ============================================
// Create blob and download
const blob = new Blob([csvContent], { type: 'text/csv;charset=utf-8;' });
const link = document.createElement('a');
const url = URL.createObjectURL(blob);
// Filename: enhanced_listing_[FINN_ID].csv
const filename = `enhanced_listing_${finnId}.csv`;
link.setAttribute('href', url);
link.setAttribute('download', filename);
link.style.visibility = 'hidden';
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
// ============================================
// DISPLAY RESULTS
// ============================================
console.log('='.repeat(70));
console.log('FINN.NO PROPERTY DATA EXTRACTOR');
console.log('='.repeat(70));
console.log('');
console.log('📋 Extracted Data:');
console.log(' Title:', propertyData.title);
console.log(' Address:', propertyData.address);
console.log(' Postcode:', propertyData.postcode || 'Not found');
console.log(' Price:', propertyData.price);
console.log(' Size:', propertyData.size);
console.log(' Link:', propertyData.link);
console.log('');
console.log('💾 CSV File:', filename);
console.log('📁 Save location: Check your browser downloads folder');
console.log(' (Suggested: Create "enhanced_listings" folder in your Property_Finder directory)');
console.log('');
if (propertyData.postcode) {
console.log('✅ Postcode found! CSV file downloaded.');
// Also copy postcode to clipboard
navigator.clipboard.writeText(propertyData.postcode).then(() => {
console.log('📋 Postcode copied to clipboard:', propertyData.postcode);
});
} else {
console.log('⚠️ Warning: No postcode found. Address may be incomplete.');
console.log(' You may need to add the postcode manually to the CSV.');
}
console.log('='.repeat(70));
return propertyData;
})();
/**
* INSTRUCTIONS FOR FOLDER SETUP:
*
* 1. In your browser, set the download folder to:
* Property_Finder/enhanced_listings/
*
* OR
*
* 2. After downloading, manually move CSV files to:
* Property_Finder/enhanced_listings/
*
* 3. Use CSVmerger.py (with modifications) to merge all enhanced_listing_*.csv files
*/