Movatterモバイル変換


[0]ホーム

URL:


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

Commit854e70a

Browse files
Added stock price fetch + package-lock.json (TheAlgorithms#233)
* Added stock price fetch* Delete package-lock.jsonCo-authored-by: vinayak <itssvinayak@gmail.com>
1 parentdf58a7e commit854e70a

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+
constfetch=require('node-fetch')
2+
constjsdom=require('jsdom')
3+
4+
// function to get the stock price from the given symbol
5+
asyncfunctiongetStockPrice(stockSymbol){
6+
// parsing the html page body
7+
consturl=`https://in.finance.yahoo.com/lookup?s=$${stockSymbol}`
8+
constresponse=awaitfetch(url)
9+
constpageBody=awaitresponse.text()
10+
constdom=newjsdom.JSDOM(pageBody,'text/html')
11+
// returning the price as a number
12+
returnparseFloat(dom.window.document.querySelectorAll('td')[2].textContent.replace(/,/g,''))
13+
}
14+
15+
asyncfunctionmain(){
16+
// Using async await to ensure synchronous behaviour
17+
awaitgetStockPrice('GOOGL')
18+
.then(response=>console.log(`GOOGL stock price: $${response}`))
19+
20+
awaitgetStockPrice('AAPL')
21+
.then(response=>console.log(`AAPL stock price: $${response}`))
22+
23+
awaitgetStockPrice('MSFT')
24+
.then(response=>console.log(`MSFT stock price: $${response}`))
25+
26+
awaitgetStockPrice('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)

[8]ページ先頭

©2009-2025 Movatter.jp