Scraping eBay Listings with JavaScript and DOM Parsing in 2023

Oct 5, 2023 ยท 2 min read

Let's go through how to scrape and extract data from eBay listings using JavaScript DOM parsing.

Setup

We'll use the request and cheerio packages:

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 to get the HTML:

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));

  });

});

Browse by language:

The easiest way to do Web Scraping

Get HTML from any page with a simple API call. We handle proxy rotation, browser identities, automatic retries, CAPTCHAs, JavaScript rendering, etc automatically for you


Try ProxiesAPI for free

curl "http://api.proxiesapi.com/?key=API_KEY&url=https://example.com"

<!doctype html>
<html>
<head>
    <title>Example Domain</title>
    <meta charset="utf-8" />
    <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
...

X

Don't leave just yet!

Enter your email below to claim your free API key: