Skip to content

Instantly share code, notes, and snippets.

@Syzygianinfern0
Last active March 26, 2024 05:27
Show Gist options
  • Save Syzygianinfern0/d60e7d3b46227d159dceb92065ec0001 to your computer and use it in GitHub Desktop.
Save Syzygianinfern0/d60e7d3b46227d159dceb92065ec0001 to your computer and use it in GitHub Desktop.
Ebay Average Selling Price Statistics

Ebay Average Selling Price Statistics

This repository contains two versions of a JavaScript script designed to analyze and display statistical information about prices listed on ebay. The script extracts prices using a specific CSS selector, calculates statistical measures such as the minimum, maximum, average, median, and mode prices, and filters out outliers.

Important

THIS ONLY WORKS FOR SOLD/COMPLETED LISTINGS

Versions

There are two versions of the script:

  1. Console Script: This version is intended for use in the developer console of a web browser. It includes descriptive comments to help understand and modify the code as needed.

  2. Bookmarklet: This version is a compacted form of the script meant to be used as a browser bookmarklet for ease of use on any webpage without needing to open the developer console.

Usage

Console Script

  1. Open the developer console on the webpage where you want to analyze prices. This is usually done by pressing F12 or right-clicking on the page and selecting "Inspect".
  2. Navigate to the Console tab.
  3. Copy and paste the entire Console Script into the console and press Enter. The results will be displayed in the console output.

Bookmarklet

  1. Create a new bookmark in your browser.
  2. Edit the bookmark and set the URL to be the entire Bookmarklet code.
  3. Save the bookmark.
  4. Click the bookmark while on the webpage you want to analyze. An alert box with the statistical data will appear.

Methodology

The script works by performing the following steps:

  1. Data Scraping: The script uses the CSS selector .s-item__price .POSITIVE to find elements on the webpage that contain pricing information. This can be adjusted to fit the specific structure of the target website.

  2. Data Cleaning: Prices are extracted as strings; the script removes any non-numeric characters (like dollar signs and commas) and converts these strings into numbers for analysis.

  3. Outlier Removal: The script calculates the Interquartile Range (IQR) and uses it to identify and remove outliers from the data set. This ensures that the statistical analysis is not skewed by extreme values.

  4. Statistical Analysis: The script calculates and displays the minimum, maximum, average, median, and mode prices from the cleaned, outlier-free dataset.

javascript:(function() {
const elements = document.querySelectorAll('.s-item__price .POSITIVE');
const texts = Array.from(elements).map(element => element.textContent);
const prices = texts
.map(text => text.replace('$', '').replace(',', ''))
.filter(text => !isNaN(parseFloat(text)))
.map(text => parseFloat(text));
prices.sort((a, b) => a - b);
const q1 = prices[Math.floor((prices.length / 4))];
const q3 = prices[Math.floor((prices.length * (3 / 4)))];
const iqr = q3 - q1;
const lowerBound = q1 - 1 * iqr;
const upperBound = q3 + 1 * iqr;
const filteredPrices = prices.filter(price => price >= lowerBound && price <= upperBound);
const minPrice = Math.min(...filteredPrices);
const maxPrice = Math.max(...filteredPrices);
const avgPrice = filteredPrices.reduce((sum, price) => sum + price, 0) / filteredPrices.length;
let medianPrice;
const midIndex = Math.floor(filteredPrices.length / 2);
if (filteredPrices.length % 2 === 0) {
medianPrice = (filteredPrices[midIndex] + filteredPrices[midIndex - 1]) / 2;
} else {
medianPrice = filteredPrices[midIndex];
}
const frequencyMap = filteredPrices.reduce((acc, price) => {
acc[price] = (acc[price] || 0) + 1;
return acc;
}, {});
let maxFrequency = 0;
let modes = [];
for (const price in frequencyMap) {
if (frequencyMap[price] > maxFrequency) {
modes = [parseFloat(price)];
maxFrequency = frequencyMap[price];
} else if (frequencyMap[price] === maxFrequency) {
modes.push(parseFloat(price));
}
}
if (modes.length === filteredPrices.length) {
modes = [];
}
alert(`Minimum price (without outliers): $${minPrice.toFixed(2)}\nMaximum price (without outliers): $${maxPrice.toFixed(2)}\nAverage price (without outliers): $${avgPrice.toFixed(2)}\nMedian price (without outliers): $${medianPrice.toFixed(2)}\nMode price (without outliers): ${modes.length ? '$' + modes.join(', $') : 'No mode'}`);
})();
const elements = document.querySelectorAll('.s-item__price .POSITIVE');
const texts = Array.from(elements).map(element => element.textContent);
// Assuming 'texts' contains your array of price strings:
const prices = texts
.map(text => text.replace('$', '').replace(',', '')) // Remove dollar signs and commas
.filter(text => !isNaN(parseFloat(text))) // Keep only the strings that can be converted to numbers
.map(text => parseFloat(text)); // Convert strings to numbers
// Assuming 'prices' contains your array of numerical prices:
prices.sort((a, b) => a - b); // Sort prices in ascending order
// Calculate Q1 and Q3
const q1 = prices[Math.floor((prices.length / 4))];
const q3 = prices[Math.floor((prices.length * (3 / 4)))];
const iqr = q3 - q1;
// Calculate the bounds for determining outliers
const lowerBound = q1 - 1 * iqr;
const upperBound = q3 + 1 * iqr;
// Filter out outliers
const filteredPrices = prices.filter(price => price >= lowerBound && price <= upperBound);
// Compute statistics on filtered data
const minPrice = Math.min(...filteredPrices);
const maxPrice = Math.max(...filteredPrices);
const avgPrice = filteredPrices.reduce((sum, price) => sum + price, 0) / filteredPrices.length;
let message = `Minimum price (without outliers): $${minPrice.toFixed(2)}`
message += `Maximum price (without outliers): $${maxPrice.toFixed(2)}`
message += `Average price (without outliers): $${avgPrice.toFixed(2)}`
// console.log(`Minimum price (without outliers): $${minPrice.toFixed(2)}`);
// console.log(`Maximum price (without outliers): $${maxPrice.toFixed(2)}`);
// console.log(`Average price (without outliers): $${avgPrice.toFixed(2)}`);
// Calculate Median
let medianPrice;
const midIndex = Math.floor(filteredPrices.length / 2);
if (filteredPrices.length % 2 === 0) { // if even
medianPrice = (filteredPrices[midIndex] + filteredPrices[midIndex - 1]) / 2;
} else { // if odd
medianPrice = filteredPrices[midIndex];
}
// Calculate Mode (most frequently occurring value)
const frequencyMap = filteredPrices.reduce((acc, price) => {
acc[price] = (acc[price] || 0) + 1;
return acc;
}, {});
let maxFrequency = 0;
let modes = [];
for (const price in frequencyMap) {
if (frequencyMap[price] > maxFrequency) {
modes = [parseFloat(price)]; // New mode(s) array with the new most frequent price
maxFrequency = frequencyMap[price];
} else if (frequencyMap[price] === maxFrequency) {
modes.push(parseFloat(price)); // Add to the modes array if frequency is the same as maxFrequency
}
}
// If every element is unique, then technically there is no mode
if (modes.length === filteredPrices.length) {
modes = [];
}
message += `Median price (without outliers): $${medianPrice.toFixed(2)}`
message += `Mode price (without outliers): ${modes.length ? '$' + modes.join(', $') : 'No mode'}`
// console.log(`Median price (without outliers): $${medianPrice.toFixed(2)}`);
// console.log(`Mode price (without outliers): ${modes.length ? '$' + modes.join(', $') : 'No mode'}`);
alert(message)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment