From f752e99fb80d0f1ebef15fa2bb4baa9af68a90e3 Mon Sep 17 00:00:00 2001 From: vansh1999 Date: Sat, 23 Nov 2019 16:54:26 +0530 Subject: [PATCH 1/2] added current stock price --- web_programming/current_stock_price.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 web_programming/current_stock_price.py diff --git a/web_programming/current_stock_price.py b/web_programming/current_stock_price.py new file mode 100644 index 000000000000..0498de9a61f3 --- /dev/null +++ b/web_programming/current_stock_price.py @@ -0,0 +1,17 @@ +import bs4 +import requests +from bs4 import BeautifulSoup + + +def parsePrice(): + page = requests.get('https://in.finance.yahoo.com/quote/%5EIXIC?p=^IXIC') + soup = bs4.BeautifulSoup(page.content , 'lxml') + price = soup.find_all('div' , {'class' : 'My(6px) Pos(r) smartphone_Mt(6px)'})[0].find_all('span')[0].text + + return price + + +while True: + print('Current price is ' + str(parsePrice())) + + \ No newline at end of file From e59c4e0ed4c25b8e2f26e61800ebb46766db8970 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sat, 23 Nov 2019 13:53:43 +0100 Subject: [PATCH 2/2] Ten lines or less --- web_programming/current_stock_price.py | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/web_programming/current_stock_price.py b/web_programming/current_stock_price.py index 0498de9a61f3..df44da4ef351 100644 --- a/web_programming/current_stock_price.py +++ b/web_programming/current_stock_price.py @@ -1,17 +1,14 @@ -import bs4 import requests from bs4 import BeautifulSoup -def parsePrice(): - page = requests.get('https://in.finance.yahoo.com/quote/%5EIXIC?p=^IXIC') - soup = bs4.BeautifulSoup(page.content , 'lxml') - price = soup.find_all('div' , {'class' : 'My(6px) Pos(r) smartphone_Mt(6px)'})[0].find_all('span')[0].text +def stock_price(symbol: str = "AAPL") -> str: + url = f"https://in.finance.yahoo.com/quote/{symbol}?s={symbol}" + soup = BeautifulSoup(requests.get(url).text, "html.parser") + class_ = "My(6px) Pos(r) smartphone_Mt(6px)" + return soup.find("div", class_=class_).find("span").text - return price - -while True: - print('Current price is ' + str(parsePrice())) - - \ No newline at end of file +if __name__ == "__main__": + for symbol in "AAPL AMZN IBM GOOG MSFT ORCL".split(): + print(f"Current {symbol:<4} stock price is {stock_price(symbol):>8}")