YQL - 找不到表的定义

问题描述:

我的代码:

import yql
y = yql.Public()
query = 'SELECT * FROM yahoo.finance.option_contracts WHERE symbol="SPY"'
y.execute(query)

结果:

yql.YQLError: No definition found for Table yahoo.finance.option_contracts

我知道该表存在,因为我可以在 http://developer.yahoo 上测试查询.com/yql/console/ 它可以工作.我错过了什么?

I know that the table exists because I can test the query at http://developer.yahoo.com/yql/console/ and it works. What am I missing?

更新:我将 url 发布到控制台,但没有发布我在控制台中尝试的查询.现在已附加查询.

Update: I posted the url to the console but not the query I tried in the console. The query is now attached.

http://goo.gl/mNXwC

由于 yahoo.finance.option_contracts 表是一个 社区开放数据表 您需要将其作为查询环境的一部分包含在内.最简单的方法是加载所有社区表的环境文件;就像在 YQL 控制台中单击显示社区表"一样.

Since the yahoo.finance.option_contracts table is a Community Open Data Table you will want to include it as part of the environment for the query. The easiest way to do that is to load up the environment file for all community tables; just like clicking "Show Community Tables" in the YQL console.

通常会通过在 YQL 查询 URL 中指定 env=... 参数,或者(如您所做的那样)在查询本身.

One would normally do that by specifying an env=... parameter in the YQL query URL, or (as you have done) with a use clause in the query itself.

您使用的 Python 库允许您将环境文件作为参数传递给 execute().

The Python library that you are using lets you pass in the environment file as an argument to execute().

import yql
y = yql.Public()
query = 'SELECT * FROM yahoo.finance.option_contracts WHERE symbol="SPY"'
y.execute(query, env="store://datatables.org/alltableswithkeys")

这是一个扩展 yql.Public 的示例,以便能够在实例化时定义默认环境.


Here's an example of extending yql.Public to be able to define the default environment on instantiation.

class MyYql(yql.Public):

    def __init__(self, api_key=None, shared_secret=None, httplib2_inst=None, env=None):
        super(MyYql, self).__init__(api_key, shared_secret, httplib2_inst)
        self.env = env if env else None

    def execute(self, query, params=None, **kwargs):
        kwargs["env"] = kwargs.get("env", self.env)
        return super(MyYql, self).execute(query, params, **kwargs);

它可以像这样使用:

y = MyYql(env="store://datatables.org/alltableswithkeys")
query = 'SELECT * FROM yahoo.finance.option_contracts WHERE symbol="SPY"'
r = y.execute(query)

如果需要,您仍然可以在对 y.execute() 的单独调用中覆盖 env.

You can still override the env in an individual call to y.execute() if you need to.