Search code examples
xmlindexingfinanceyqlstockquotes

Getting component of stock market index from YQL


Currently, I can get stock quote by returning xml and json using YQL console like

select symbol, price from csv where url='http://download.finance.yahoo.com/d/quotes.csv?s=IBM,YHOO,GOOG,MSFT&f=sl1d1t1c1ohgv&e=.csv' and columns='symbol,price,date,time,change,col1,high,low,col2'

I would like to get the complete component list from

http://download.finance.yahoo.com/d/quotes.csv?s=@^HSI&f=sl1d1t1c1ohgv&e=.csv

using YQL console, so I input the statement below in YQL console

select symbol, price from csv where url='http://download.finance.yahoo.com/d/quotes.csv?s=@^HSI&f=sl1d1t1c1ohgv&e=.csv' and columns='symbol,price,date,time,change,col1,high,low,col2'

but it said my link is invalid. Any thoughts and alterative solutions?


Solution

  • Your query was very nearly correct, but the URL was considered "invalid" as you noted. The solution is to properly escape the query string values.

    http://download.finance.yahoo.com/d/quotes.csv?s=@^HSI&f=sl1d1t1c1ohgv&e=.csv
    

    becomes

    http://download.finance.yahoo.com/d/quotes.csv?s=%40%5EHSI&f=sl1d1t1c1ohgv&e=.csv
    

    Changing just those two characters into their %-encoded values allows YQL to pull back the CSV data.

    select * from csv where url='http://download.finance.yahoo.com/d/quotes.csv?s=%40%5EHSI&f=sl1d1t1c1ohgv&e=.csv'
    

    Aside: YQL doesn't like that the CSV has an empty line at the end of the file, this will cause issues when you try to use the columns where clause. If you're okay with having the columns called col<number> and want to skip the last (empty) row then use and col8 is not null at the end of your query.