使用 Python & BeautifulSoup 从 Yahoo Finance 抓取股票价格
Scraping stock price from Yahoo Finance using Python & BeautifulSoup
我正在尝试使用 Python 和 BeautifulSoup 从 Yahoo Finance 抓取 股票价格。但是,我无法获取具有特定“data-reactid”属性的标签(参见屏幕截图)。请帮助我。
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.
属性 data-reactid
本质上是动态的,因此您无法使用它真正找出 dom 元素。
试试这个:
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()))
我正在尝试使用 Python 和 BeautifulSoup 从 Yahoo Finance 抓取 股票价格。但是,我无法获取具有特定“data-reactid”属性的标签(参见屏幕截图)。请帮助我。
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.
属性 data-reactid
本质上是动态的,因此您无法使用它真正找出 dom 元素。
试试这个:
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()))