Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 854e70a

Browse files
ruppysuppyitsvinayak
andauthored
Added stock price fetch + package-lock.json (TheAlgorithms#233)
* Added stock price fetch * Delete package-lock.json Co-authored-by: vinayak <itssvinayak@gmail.com>
1 parent df58a7e commit 854e70a

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

‎Web-Programming/StockPrice.js‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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()

‎package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"author": "TheAlgorithms",
99
"license": "GPL-3.0",
1010
"dependencies": {
11+
"jsdom": "^16.3.0",
1112
"node-fetch": "2.6.0"
1213
},
1314
"devDependencies": {

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /