Created
February 3, 2021 22:27
-
-
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)
This file contains hidden or 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 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