Skip to content

updated the URL and HTML tags for scrapping yahoo finance #8942

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 12, 2023
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions web_programming/current_stock_price.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,20 @@


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
url = f"https://finance.yahoo.com/quote/{symbol}?p={symbol}"
yahoo_finance_source = requests.get(url, headers={"USER-AGENT": "Mozilla/5.0"}).text
soup = BeautifulSoup(yahoo_finance_source, "html.parser")
specific_fin_streamer_tag = soup.find("fin-streamer", {"data-test": "qsp-price"})

if specific_fin_streamer_tag:
text = specific_fin_streamer_tag.get_text()
return text
else:
print("No <fin-streamer> tag with the specified data-test attribute found.")
return "Not Found"


# Search for the symbol at https://finance.yahoo.com/lookup
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}")