Skip to content

Instantly share code, notes, and snippets.

@nax3t
Created February 3, 2021 22:27
Show Gist options
  • Save nax3t/3125925ef5daaed992a2ff291e566e49 to your computer and use it in GitHub Desktop.
Save nax3t/3125925ef5daaed992a2ff291e566e49 to your computer and use it in GitHub Desktop.
Web scraper program for getting current stock or crypto prices (e.g., GME, AMC, BTC-USD)
const axios = require('axios')
const cheerio = require('cheerio')
async function getPrice(symbol) {
try {
let { data } = await axios.get(`https://finance.yahoo.com/quote/${symbol}?p=${symbol}&.tsrc=fin-srch`)
let $ = cheerio.load(data)
// use this one for bitcoin
// console.log(`The current price of ${symbol} is:`, '$' + $('span[data-reactid="33"]').eq(1).text())
// use this one for aftermarket hours
// console.log(`The current price of ${symbol} is:`, $('span[data-reactid="55"]').first().text())
// use this one for regular market hours
console.log(`The current price of ${symbol} is:`, $('span[data-reactid="50"]').first().text())
} catch(err) {
throw error
}
}
setInterval(() => {
getPrice('GME')
}, 5000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment