【发布时间】:2011-09-22 13:58:38
【问题描述】:
我正在迁移一些 Yahoo Finance CSV/屏幕抓取界面以使用 YQL,并在 yahoo.finance.options 表中苦苦挣扎。如果我查询给定符号的所有选项,我找不到与选项关联的到期日期。如果我查询符号和到期日期,那么我会找到与链相关的到期日期,但不是其中的选项。虽然我熟悉期权到期的周期并且可以从给定日期引导它,但这是一个糟糕的解决方案;一方面,它会产生更多的查询。我更喜欢从数据中反省它,因为它应该是可用的(它可以被屏幕抓取)。有人知道如何在 YQL 中解决这个问题,还是我不走运?
这是我正在使用的一些 python 代码:
from xml.etree.ElementTree import ElementTree
import urllib, urllib2
class YQL(object):
url = 'http://query.yahooapis.com/v1/public/yql'
env = 'store://datatables.org/alltableswithkeys'
format = 'xml'
@classmethod
def query(cls, string):
q = urllib.quote(string)
url = cls.url + '&'.join(('?q=%s' % q, 'env=%s' % cls.env,
'format=%s' % cls.format))
resp = urllib2.urlopen(url)
return ElementTree(file=resp).getroot().find('results')[:]
chain = YQL.query('select * from yahoo.finance.options where symbol="WFC"')[0]
chain.attrib
option = chain[0]
option.attrib
for attr in option:
print attr.tag, attr.text
【问题讨论】: