Skip to content

Instantly share code, notes, and snippets.

@chadoh
Last active January 16, 2020 13:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chadoh/c6509f443d45ba26283786715f8203ee to your computer and use it in GitHub Desktop.
Save chadoh/c6509f443d45ba26283786715f8203ee to your computer and use it in GitHub Desktop.
node script to search ebay for a search term and print items + prices to your terminal
const jsdom = require("jsdom")
const { JSDOM } = jsdom
const fetch = require('node-fetch');
const newItemRegex = new RegExp('<span class="LIGHT_HIGHLIGHT">New Listing</span>')
const scrapeEbaySearchResults = dom => console.log(
Array.from(
dom.querySelectorAll('ul.srp-results li.s-item')
).map(li => ({
title: li.querySelector('h3').innerHTML.replace(newItemRegex, ''),
price: li.querySelector('.s-item__price').innerHTML,
}))
)
const getItems = query => {
fetch(`https://www.ebay.com/sch/i.html?_from=R40&_nkw=${encodeURI(query)}&_sacat=0`)
.then(res => res.text())
.then(body => {
const { document } = (new JSDOM(body)).window
scrapeEbaySearchResults(document)
})
}
getItems(process.argv.slice(2).join(' '))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment