Issue
I'm trying to scrape stock price from Yahoo Finance using Python and BeautifulSoup. However, I'm not able to fetch the tag having a specific data-reactid
attribute (See the screenshot). Please help me.
Code:
def getCurrentPrice(self, stockSymbol):
#stockSymbol is : MSFT for Microsoft
url = "https://finance.yahoo.com/quote/{}".format(stockSymbol)
source = requests.get(url).text
soup = BeautifulSoup(source, 'lxml')
currentPrice = soup.find('span',attrs={"data-reactid": "52"})
print("{} : {}".format(stockSymbol, currentPrice))
Output:
MSFT : None # None because the span tag is not being found.
Solution
The attribute data-reactid
is dynamic in nature so you can't really find out the dom element using that.
Try this:
def getCurrentPrice(self, stockSymbol):
#stockSymbol is : MSFT for Microsoft
url = "https://finance.yahoo.com/quote/{}".format(stockSymbol)
source = requests.get(url).text
soup = BeautifulSoup(source, 'lxml')
currentPrice = soup.find('span',attrs={"class": "Trsdu(0.3s)"})
print("{} : {}".format(stockSymbol, currentPrice.get_text()))
Answered By - Shubham Sharma
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.