Scrape Yahoo Finance Financial Ratios
<p>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.</p>

<p><a href="https://i.stack.imgur.com/SKunN.jpg" rel="nofollow"><img src="https://i.stack.imgur.com/SKunN.jpg" alt="enter image description here"></a></p>

<p>Interestingly, when I look at the Page Source of the URL, the value of the Current Ratio is not listed there.</p>

<p>My code so far is:</p>

<pre><code>import urllib
from bs4 import BeautifulSoup

url = ("http://finance.yahoo.com/quote/GSB/key-statistics?p=GSB")
html = urllib.urlopen(url).read()
soup = BeautifulSoup(html, "html.parser")
script = soup.find("td", {"class": "Fz(s) Fw(500) Ta(end)",
                          "data-reactid": ".1ujetg16lcg.0.$0.0.0.3.1.$main-0-Quote-Proxy.$main-0-Quote.2.0.0.0.1.0.1:$FINANCIAL_HIGHLIGHTS.$BALANCE_SHEET.1.0.$CURRENT_RATIO.1"
                         })
</code></pre>

<p>Does anyone know how to solve this?</p>

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

<pre><code>import requests
a = requests.get('https://query2.finance.yahoo.com/v10/finance/quoteSummary/GSB?formatted=true&amp;crumb=A7e5%2FXKKAFa&amp;lang=en-US&amp;region=US&amp;modules=defaultKeyStatistics%2CfinancialData%2CcalendarEvents&amp;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
</code></pre>

<p>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 <code>pprint</code>. Furthermore, for the pages that have quarterly information just change <code>[0]</code> to <code>[1]</code> to get the info for the second quarter instead of the first.. and so on and so forth. </p>

python beautifulsoup