Last active
January 16, 2020 13:28
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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