Python, yahoo yql quote error

I used the yql console and got the appropriate answer. However, when submitting a python based request, I keep getting the error. First a console example:

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

      

I am getting a result block with the expected fields. 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 answer:

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

What am I missing? TIA, Clayton

+3


source to share


1 answer


What you are missing is part of the request:

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'}]}}}

      

+3


source







All Articles