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
+
2
3
from bs4 import BeautifulSoup
4
+ from requests import get
3
5
4
6
5
7
def stock_price (symbol : str = "AAPL" ) -> str :
8
+ """
9
+ >>> stock_price("EEEE")
10
+ '-'
11
+ >>> isinstance(float(stock_price("GOOG")),float)
12
+ True
13
+ """
6
14
url = f"https://finance.yahoo.com/quote/{ symbol } ?p={ symbol } "
7
- yahoo_finance_source = requests . get (
15
+ yahoo_finance_source = get (
8
16
url , headers = {"USER-AGENT" : "Mozilla/5.0" }, timeout = 10
9
17
).text
10
18
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" })
12
20
13
21
if specific_fin_streamer_tag :
14
22
text = specific_fin_streamer_tag .get_text ()
@@ -18,5 +26,6 @@ def stock_price(symbol: str = "AAPL") -> str:
18
26
19
27
# Search for the symbol at https://finance.yahoo.com/lookup
20
28
if __name__ == "__main__" :
29
+ testmod ()
21
30
for symbol in "AAPL AMZN IBM GOOG MSFT ORCL" .split ():
22
31
print (f"Current { symbol :<4} stock price is { stock_price (symbol ):>8} " )
You can’t perform that action at this time.
0 commit comments