Scrape Yahoo Finance Financial Ratios

后端 未结 4 883
清歌不尽
清歌不尽 2021-02-04 19:52

I have been trying to scrap the value of the Current Ratio (as shown below) from Yahoo Finance using Beautiful Soup, but it keeps returning an empty value.

Inte

4条回答
  •  生来不讨喜
    2021-02-04 20:12

    One thing I'd add to Padriac's answer is to except KeyErrors, since you'll probably be scraping more than one ticker.

    import requests
    a = requests.get('https://query2.finance.yahoo.com/v10/finance/quoteSummary/GSB?formatted=true&crumb=A7e5%2FXKKAFa&lang=en-US®ion=US&modules=defaultKeyStatistics%2CfinancialData%2CcalendarEvents&corsDomain=finance.yahoo.com')
    b = a.json()
    try:
        ratio = b['quoteSummary']['result'][0]['financialData']['currentRatio']['raw']
        print(ratio) #prints 1.974
    except (IndexError, KeyError):
        pass
    

    A cool thing about doing it like this is that you can easily change the keys for the information you want. A good way to see the way the dictionary is nested on the Yahoo! Finance pages is to use pprint. Furthermore, for the pages that have quarterly information just change [0] to [1] to get the info for the second quarter instead of the first.. and so on and so forth.

提交回复
热议问题