2

我使用了 yql 控制台并收到了适当的回复。但是,发送基于 python 的查询,我仍然有一个错误。首先是控制台示例:

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

我收到一个包含预期字段的结果块。蟒蛇示例:

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)

回复如下:

b'\n找不到表 yahoo.finance.quotes' 的定义

我错过了什么?TIA,克莱顿

4

1 回答 1

3

您缺少的是查询的 env 部分:

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)

输出:

{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'}]}}}
于 2014-12-06T03:47:37.537 回答