代码如下,总是执行出错。视频的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
fw = open(filename, "w")注意缩进
不是whitespace的问题。。。
@charles999: request这个模块对https不支持,你开http的是可以的,开https的url用requests吧
你看的是哪个书的视频,兄弟,能否发一份到hbsoft2008@sina.com
我在youtube看的,B站也有Python 3.4 Programming Tutorials