2017-09-16 62 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)。