使用Python和BeautifulSoup从Yahoo Finance抓取股票价格

时间:2020-03-27 15:52:55

标签: python web-scraping beautifulsoup yahoo-finance

我正在尝试使用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.

enter image description here

1 个答案:

答案 0 :(得分:2)

属性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()))