mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-05 16:26:47 +08:00
Added stock price fetch + package-lock.json (#233)
* Added stock price fetch * Delete package-lock.json Co-authored-by: vinayak <itssvinayak@gmail.com>
This commit is contained in:
30
Web-Programming/StockPrice.js
Normal file
30
Web-Programming/StockPrice.js
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
const fetch = require('node-fetch')
|
||||||
|
const jsdom = require('jsdom')
|
||||||
|
|
||||||
|
// function to get the stock price from the given symbol
|
||||||
|
async function getStockPrice (stockSymbol) {
|
||||||
|
// parsing the html page body
|
||||||
|
const url = `https://in.finance.yahoo.com/lookup?s=$${stockSymbol}`
|
||||||
|
const response = await fetch(url)
|
||||||
|
const pageBody = await response.text()
|
||||||
|
const dom = new jsdom.JSDOM(pageBody, 'text/html')
|
||||||
|
// returning the price as a number
|
||||||
|
return parseFloat(dom.window.document.querySelectorAll('td')[2].textContent.replace(/,/g, ''))
|
||||||
|
}
|
||||||
|
|
||||||
|
async function main () {
|
||||||
|
// Using async await to ensure synchronous behaviour
|
||||||
|
await getStockPrice('GOOGL')
|
||||||
|
.then(response => console.log(`GOOGL stock price: $ ${response}`))
|
||||||
|
|
||||||
|
await getStockPrice('AAPL')
|
||||||
|
.then(response => console.log(`AAPL stock price: $ ${response}`))
|
||||||
|
|
||||||
|
await getStockPrice('MSFT')
|
||||||
|
.then(response => console.log(`MSFT stock price: $ ${response}`))
|
||||||
|
|
||||||
|
await getStockPrice('AMZN')
|
||||||
|
.then(response => console.log(`AMZN stock price: $ ${response}`))
|
||||||
|
}
|
||||||
|
|
||||||
|
main()
|
@ -8,6 +8,7 @@
|
|||||||
"author": "TheAlgorithms",
|
"author": "TheAlgorithms",
|
||||||
"license": "GPL-3.0",
|
"license": "GPL-3.0",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"jsdom": "^16.3.0",
|
||||||
"node-fetch": "2.6.0"
|
"node-fetch": "2.6.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
Reference in New Issue
Block a user