Search code examples
pythonyahoo-apistockquotes

Python, yahoo yql quote error


I have used the yql Console and have received an appropriate response. However, sending a python based query, I continue to have an error. First the console example:

select * from yahoo.finance.quotes where symbol in ("yahoo", "aapl")

I receive a results block that has the expected fields. The python example:

import requests
base_url = 'https://query.yahooapis.com/v1/public/yql?'
query = 'q=select * from yahoo.finance.quotes where symbol in ("YHOO", "AAPL")'
full_query=base_url + query
result = requests.get(full_query)
print(result.content)

With the following response:

b'\nNo definition found for Table yahoo.finance.quotes'

What am I missing? TIA, Clayton


Solution

  • What you are missing is the env part of the query:

    import json
    import urllib
    from pprint import pprint
    
    base_url = 'https://query.yahooapis.com/v1/public/yql?'
    query = {
        'q': 'select * from yahoo.finance.quote where symbol in ("YHOO","AAPL")',
        'format': 'json',
        'env': 'store://datatables.org/alltableswithkeys'
    }
    
    url = base_url + urllib.urlencode(query)
    response = urllib.urlopen(url)
    data = response.read()
    quote = json.loads(data)
    pprint(quote)
    

    Output:

    {u'query': {u'count': 2,
                u'created': u'2014-12-06T03:53:23Z',
                u'lang': u'en-US',
                u'results': {u'quote': [{u'AverageDailyVolume': u'38009400',
                                         u'Change': u'+0.58',
                                         u'DaysHigh': u'51.25',
                                         u'DaysLow': u'50.51',
                                         u'DaysRange': u'50.51 - 51.25',
                                         u'LastTradePriceOnly': u'50.99',
                                         u'MarketCapitalization': u'48.305B',
                                         u'Name': u'Yahoo! Inc.',
                                         u'StockExchange': u'NasdaqNM',
                                         u'Symbol': u'YHOO',
                                         u'Volume': u'15418123',
                                         u'YearHigh': u'52.62',
                                         u'YearLow': u'32.15',
                                         u'symbol': u'YHOO'},
                                        {u'AverageDailyVolume': u'57049800',
                                         u'Change': u'-0.49',
                                         u'DaysHigh': u'116.08',
                                         u'DaysLow': u'114.64',
                                         u'DaysRange': u'114.64 - 116.08',
                                         u'LastTradePriceOnly': u'115.00',
                                         u'MarketCapitalization': u'674.5B',
                                         u'Name': u'Apple Inc.',
                                         u'StockExchange': u'NasdaqNM',
                                         u'Symbol': u'AAPL',
                                         u'Volume': u'38318896',
                                         u'YearHigh': u'119.75',
                                         u'YearLow': u'70.5071',
                                         u'symbol': u'AAPL'}]}}}