首页 新闻 会员 周边 捐助

Python,从网络下载csv文件

0
[已解决问题] 解决于 2017-08-24 17:31
代码如下,总是执行出错。视频的Python版本是3.4,我的事3.6
from urllib import request

goog_url = "https://query1.finance.yahoo.com/v7/finance/download/GOOG?period1=1500721097&period2=1503399497&interval=1d&events=history&crumb=EoXe3TOhKsy"

def download(csv_url):
response = request.urlopen(csv_url)
csv = response.read()
csv_str = str(csv)
lines = csv_str.split("\\n")
filename = r"goog.csv"
fw = open(filename, "w")
for line in lines:
fw.write(line+'\n')
fw.close()

download(goog_url)

请问到底是为什么呢
问题补充:

执行的错误信息:

Traceback (most recent call last):
  File "C:/Users/30478/PycharmProjects/YouTubeTutorial/main.py", line 16, in <module>
    download(goog_url)
  File "C:/Users/30478/PycharmProjects/YouTubeTutorial/main.py", line 6, in download
    response = request.urlopen(csv_url)
  File "C:\Users\30478\AppData\Local\Programs\Python\Python36\lib\urllib\request.py", line 223, in urlopen
    return opener.open(url, data, timeout)
  File "C:\Users\30478\AppData\Local\Programs\Python\Python36\lib\urllib\request.py", line 532, in open
    response = meth(req, response)
  File "C:\Users\30478\AppData\Local\Programs\Python\Python36\lib\urllib\request.py", line 642, in http_response
    'http', request, response, code, msg, hdrs)
  File "C:\Users\30478\AppData\Local\Programs\Python\Python36\lib\urllib\request.py", line 570, in error
    return self._call_chain(*args)
  File "C:\Users\30478\AppData\Local\Programs\Python\Python36\lib\urllib\request.py", line 504, in _call_chain
    result = func(*args)
  File "C:\Users\30478\AppData\Local\Programs\Python\Python36\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

charles999的主页 charles999 | 菜鸟二级 | 园豆:204
提问于:2017-08-22 19:23
< >
分享
最佳答案
0

fw = open(filename, "w")注意缩进

奖励园豆:5
北方姆Q | 小虾三级 |园豆:1120 | 2017-08-23 10:13

不是whitespace的问题。。。

charles999 | 园豆:204 (菜鸟二级) | 2017-08-23 19:46

@charles999: request这个模块对https不支持,你开http的是可以的,开https的url用requests吧

北方姆Q | 园豆:1120 (小虾三级) | 2017-08-24 10:11
其他回答(1)
0

你看的是哪个书的视频,兄弟,能否发一份到hbsoft2008@sina.com

男人要爽 | 园豆:6 (初学一级) | 2017-08-23 09:54

我在youtube看的,B站也有Python 3.4 Programming Tutorials

支持(0) 反对(0) charles999 | 园豆:204 (菜鸟二级) | 2017-08-23 19:43
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册