2017-09-16 61 views
0

我正在學習Pandas,並嘗試使用Morningstar API從Morningstar下載.csv。如何使用Python Pandas讀取和格式化Web鏈接.csv

有關於如何使用已經在這裏提供的API(雖然他們沒有具體的Python)的一些非常好放在一起說明... https://gist.github.com/hahnicity/45323026693cdde6a116

樣品的網站鏈接是hahnicity使用的例子是: http://globalquote.morningstar.com/globalcomponent/RealtimeHistoricalStockData.ashx?ticker=F&showVol=true&dtype=his&f=d&curry=USD&range=1900-1-1|2014-10-10&isD=true&isS=true&hasF=true&ProdCode=DIRECT

我的代碼是:

import pandas as pd  
path='http://globalquote.morningstar.com/globalcomponent/RealtimeHistoricalStockData.ashx?ticker=F&showVol=true&dtype=his&f=d&curry=USD&range=1900-1-1|2014-10-10&isD=true&isS=true&hasF=true&ProdCode=DIRECT' 

df=pd.read_csv(path) 

但是,在返回的是亂碼。我不確定如何讓Pandas以正確的列表形式閱讀此內容?

任何幫助是極大的讚賞。先謝謝你!

+0

當我嘗試該URL時,它只是掛起。你得到了什麼? –

+0

掛起約20秒鐘,然後下載。 – Clay

+0

您可以嘗試: path ='http://globalquote.morningstar.com/globalcomponent/RealtimeHistoricalStockData.ashx?ticker = F&showVol = true&dtype = his&f = d&curry = USD&range = 2013-1-1 | 2014-10-10&isD = true&isS = true&hasF = true&ProdCode = DIRECT' – Clay

回答

0

好吧,所以我不明白如何處理與Pandas一起存儲爲.csv的JSON格式......所以我決定改用Quandl。它解決了我的問題。

import pandas as pd 
df=pd.read_csv('https://www.quandl.com/api/v3/datasets/WIKI/<ticker_symbol>/data.csv?api_key=<api_key>') 

這將返回有問題的代碼的當日定價結束。

你可以在這裏找到文檔並註冊一個API密鑰: (https://docs.quandl.com/docs/in-depth-usage)。