Let's go through how to scrape and extract data from eBay listings using JavaScript DOM parsing.
Setup
We'll use the
npm install request cheerio
And require them:
const request = require('request');
const cheerio = require('cheerio');
Define the eBay URL and headers:
const url = '<https://www.ebay.com/sch/i.html?_nkw=baseball>';
const headers = {
'User-Agent': 'Mozilla/5.0...' // Use your browser UA string
};
Fetch Listings Page
Use
request({url, headers}, (err, resp, html) => {
// Parse HTML here
});
Load into Cheerio to parse:
const $ = cheerio.load(html);
Extract Listing Data
Find listing items and loop through them:
$('.s-item__info').each((i, elem) => {
const title = $(elem).find('.s-item__title').text().trim();
const url = $(elem).find('.s-item__link').attr('href');
const price = $(elem).find('.s-item__price').text().trim();
// Extract other fields like details, seller, etc.
});
Use jQuery-style selectors to extract text and attributes.
Print Results
console.log('Title: ' + title);
console.log('URL: ' + url);
console.log('Price: ' + price);
console.log('='.repeat(50)); // Separator
Full Code
const request = require('request');
const cheerio = require('cheerio');
const url = '<https://www.ebay.com/sch/i.html?_nkw=baseball>';
const headers = {
'User-Agent': 'Mozilla/5.0...'
};
request({url, headers}, (err, resp, html) => {
const $ = cheerio.load(html);
$('.s-item__info').each((i, elem) => {
const title = $(elem).find('.s-item__title').text().trim();
const url = $(elem).find('.s-item__link').attr('href');
const price = $(elem).find('.s-item__price').text().trim();
const details = $(elem).find('.s-item__subtitle').text().trim();
const seller = $(elem).find('.s-item__seller-info-text').text().trim();
const shipping = $(elem).find('.s-item__shipping').text().trim();
const location = $(elem).find('.s-item__location').text().trim();
const sold = $(elem).find('.s-item__quantity-sold').text().trim();
console.log('Title: ' + title);
console.log('URL: ' + url);
console.log('Price: ' + price);
console.log('Details: ' + details);
console.log('Seller: ' + seller);
console.log('Shipping: ' + shipping);
console.log('Location: ' + location);
console.log('Sold: ' + sold);
console.log('='.repeat(50));
});
});