File tree Expand file tree Collapse file tree 1 file changed +12
-3
lines changed
Expand file tree Collapse file tree 1 file changed +12
-3
lines changed Original file line number Diff line number Diff line change 1- import requests
1+ from doctest import testmod
2+
23from bs4 import BeautifulSoup
4+ from requests import get
35
46
57def stock_price (symbol : str = "AAPL" ) -> str :
8+ """
9+ >>> stock_price("EEEE")
10+ '-'
11+ >>> isinstance(float(stock_price("GOOG")),float)
12+ True
13+ """
614 url = f"https://finance.yahoo.com/quote/{ symbol } ?p={ symbol } "
7- yahoo_finance_source = requests . get (
15+ yahoo_finance_source = get (
816 url , headers = {"USER-AGENT" : "Mozilla/5.0" }, timeout = 10
917 ).text
1018 soup = BeautifulSoup (yahoo_finance_source , "html.parser" )
11- specific_fin_streamer_tag = soup .find ("fin-streamer" , {"data-test " : "qsp-price" })
19+ specific_fin_streamer_tag = soup .find ("fin-streamer" , {"data-testid " : "qsp-price" })
1220
1321 if specific_fin_streamer_tag :
1422 text = specific_fin_streamer_tag .get_text ()
@@ -18,5 +26,6 @@ def stock_price(symbol: str = "AAPL") -> str:
1826
1927# Search for the symbol at https://finance.yahoo.com/lookup
2028if __name__ == "__main__" :
29+ testmod ()
2130 for symbol in "AAPL AMZN IBM GOOG MSFT ORCL" .split ():
2231 print (f"Current { symbol :<4} stock price is { stock_price (symbol ):>8} " )
You can’t perform that action at this time.
0 commit comments