如何使用python将csv文件下载到目录

时间:2017-09-17 10:21:42

标签: python csv

我试图从雅虎查询中下载AAPL的历史股票价格,但失败了。我是python的新手,不知道应用哪种方法将指定的链接自动解压缩到我想要的本地目录。

  
    

pullData(" AAPL")结果如下:     AAPL     https://query1.finance.yahoo.com/v7/finance/download/AAPL?period1=345398400&period2=1505577600&interval=1d&events=history&crumb=X44gAJPyoNu     主循环HTTP错误401:未经授权

  
import urllib2
import time
import csv
import requests
import pandas as pd

def pullData(stock):
    try:
        print stock
        url = 'https://query1.finance.yahoo.com/v7/finance/download/'+stock+'?period1=345398400&period2=1505577600&interval=1d&events=history&crumb=X44gAJPyoNu'
        print url

        response = urllib2.urlopen(url)
        html = response.read()


    except Exception,e:
        print 'main loop',str(e)

1 个答案:

答案 0 :(得分:0)

您应该使用此修复程序更新pandas datareader https://github.com/pydata/pandas-datareader/issues/315

import pandas as pd
from pandas_datareader import data as web

start = datetime.datetime(2015, 7, 1)
end = datetime.datetime(2016, 7, 1)
data - web.DataReader('AAPL', 'yahoo',start, end)