w3resource

Python Projects: Project to get the current stock price of specified companies

Python Web Project-4 with Solution

Create a Python project to get the current stock price of specified companies.

Get company symbol from:
https://www.nyse.com/listings_directory/stock

Sample Output: (24/01/2021)

Current AAPL stock price is:    139.07
Current AMZN stock price is:  3,292.23
Current IBM  stock price is:    118.61
Current GOOG stock price is:  1,901.05
Current MSFT stock price is:    225.95
Current ORCL stock price is:     60.36
Current ABM  stock price is:     39.69
Current A    stock price is:    126.33

Sample Solution:

Python Code:

#Source: https://bit.ly/2KHn1ZW
import requests
from bs4 import BeautifulSoup

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

if __name__ == "__main__":
    for symbol in "AAPL AMZN IBM GOOG MSFT ORCL ABM A".split():
        print(f"Current {symbol:<4} stock price is:  {stock_price(symbol):>8}")

Flowchart:

Flowchart: Project to get the current stock price of specified companies.

 

Improve this sample solutions and post your code through Disqus



Follow us on Facebook and Twitter for latest update.