File tree Expand file tree Collapse file tree 2 files changed +31
-0
lines changed Expand file tree Collapse file tree 2 files changed +31
-0
lines changed Original file line number Diff line number Diff line change 1+ const fetch = require ( 'node-fetch' )
2+ const jsdom = require ( 'jsdom' )
3+
4+ // function to get the stock price from the given symbol
5+ async function getStockPrice ( stockSymbol ) {
6+ // parsing the html page body
7+ const url = `https://in.finance.yahoo.com/lookup?s=$${ stockSymbol } `
8+ const response = await fetch ( url )
9+ const pageBody = await response . text ( )
10+ const dom = new jsdom . JSDOM ( pageBody , 'text/html' )
11+ // returning the price as a number
12+ return parseFloat ( dom . window . document . querySelectorAll ( 'td' ) [ 2 ] . textContent . replace ( / , / g, '' ) )
13+ }
14+
15+ async function main ( ) {
16+ // Using async await to ensure synchronous behaviour
17+ await getStockPrice ( 'GOOGL' )
18+ . then ( response => console . log ( `GOOGL stock price: $ ${ response } ` ) )
19+
20+ await getStockPrice ( 'AAPL' )
21+ . then ( response => console . log ( `AAPL stock price: $ ${ response } ` ) )
22+
23+ await getStockPrice ( 'MSFT' )
24+ . then ( response => console . log ( `MSFT stock price: $ ${ response } ` ) )
25+
26+ await getStockPrice ( 'AMZN' )
27+ . then ( response => console . log ( `AMZN stock price: $ ${ response } ` ) )
28+ }
29+
30+ main ( )
Original file line number Diff line number Diff line change 88 "author" : " TheAlgorithms" ,
99 "license" : " GPL-3.0" ,
1010 "dependencies" : {
11+ "jsdom" : " ^16.3.0" ,
1112 "node-fetch" : " 2.6.0"
1213 },
1314 "devDependencies" : {
You can’t perform that action at this time.
0 commit comments