尝试从在线下载csv文件时出现Python代码错误

时间:2017-09-14 15:58:22

标签: python

当我尝试运行以下代码从网上读取csv文件并将数据写入文件时,我遇到了一些错误。请帮我修复此错误。我已经在下面的代码中完成了所有可能的更改,无法修复它,请帮助

from urllib import request

url = 'https://query1.finance.yahoo.com/v7/finance/download/GOOG?period1=1502724661&period2=1505403061&interval=1d&events=history&crumb=Vd9ebILPr4I'

def downloaderd(url1):
    var1 = request.urlopen(url1)
    var2 = var1.read()
    var3 = str(var2)
    lines = var3.split("\\n")

    fw = open(r"file1.txt", "w")

    for line in lines:
        fw.write(line + "\n")
    fw.close()

downloaderd(url)



 ======================================================================
    **Error Part**
    ======================================================================
    G:\Python\python.exe "C:/Users/manan/PycharmProjects/Python tutorial/main.py"
    Traceback (most recent call last):
      File "C:/Users/manan/PycharmProjects/Python tutorial/main.py", line 19, in <module>
        downloaderd(url)
      File "C:/Users/manan/PycharmProjects/Python tutorial/main.py", line 7, in downloaderd
        var1 = request.urlopen(url1)
      File "G:\Python\lib\urllib\request.py", line 223, in urlopen
        return opener.open(url, data, timeout)
      File "G:\Python\lib\urllib\request.py", line 532, in open
        response = meth(req, response)
      File "G:\Python\lib\urllib\request.py", line 642, in http_response
        'http', request, response, code, msg, hdrs)
      File "G:\Python\lib\urllib\request.py", line 570, in error
        return self._call_chain(*args)
      File "G:\Python\lib\urllib\request.py", line 504, in _call_chain
        result = func(*args)
      File "G:\Python\lib\urllib\request.py", line 650, in http_error_default
        raise HTTPError(req.full_url, code, msg, hdrs, fp)
    urllib.error.HTTPError: HTTP Error 401: Unauthorized

    Process finished with exit code 1

0 个答案:

没有答案