嘿,我正在尝试从印度的nse网站下载股票数据
所以我正在使用python这个
链接
import urllib
urllib.urlretrieve("https://www.nseindia.com/content/historical/DERIVATIVES/2016/JAN/fo01JAN2016bhav.csv.zip","fo01JAN2016bhav.csv.zip")
但是当我尝试打开下载的文件时,它会说
compressed zipped file is invalid
当我尝试从网站上正常下载时,只需粘贴链接即可打开下载的文件
链接
https://www.nseindia.com/content/historical/DERIVATIVES/2016/JAN/fo01JAN2016bhav.csv.zip
所以,如果我尝试使用urllib 2 我明白这个
f=urllib2.urlopen('https://www.nseindia.com/content/historical/DERIVATIVES/2016/JAN/fo01JAN2016bhav.csv.zip')
Traceback (most recent call last):
File "<pyshell#6>", line 1, in <module>
f=urllib2.urlopen('https://www.nseindia.com/content/historical/DERIVATIVES/2016/JAN/fo01JAN2016bhav.csv.zip')
File "C:\Python27\lib\urllib2.py", line 127, in urlopen
return _opener.open(url, data, timeout)
File "C:\Python27\lib\urllib2.py", line 410, in open
response = meth(req, response)
File "C:\Python27\lib\urllib2.py", line 523, in http_response
'http', request, response, code, msg, hdrs)
File "C:\Python27\lib\urllib2.py", line 448, in error
return self._call_chain(*args)
File "C:\Python27\lib\urllib2.py", line 382, in _call_chain
result = func(*args)
File "C:\Python27\lib\urllib2.py", line 531, in http_error_default
raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
HTTPError: HTTP Error 403: Forbidden
我该如何解决这个问题?
只有我尝试从imgur下载图像并且代码工作正常才会发生此链接为什么我可以通过浏览器正常访问http 403错误?
答案 0 :(得分:1)
此链接提供了您要执行的操作的示例:https://stackoverflow.com/a/22776/6595777
发现有关下载zip文件的另一个问题。试试这个:
url = "http://www.nseindia.com/content/historical/DERIVATIVES/2016/JAN/fo01JAN2016bhav.csv.zip"
download = urllib2.urlopen(url)
with open(os.path.basename(url), "wb") as f:
f.write(download.read())
我还没有评论权限,所以我发帖作为答案。 我无法通过https浏览您的链接,http可以正常工作。您是否尝试将脚本中的链接更改为http?
您的脚本可能正在下载我在尝试使用https(ERR_SSL_PROTOCOL_ERROR
时获得的错误页面。)这意味着您下载的内容将具有您指定的文件名(以{{1}结尾) },)但它实际上是html。这意味着它将为您提供zip文件无效的错误
答案 1 :(得分:0)
r = requests.get(url)
with open("code3.zip", "wb") as code:
code.write(r.content)
它有效
这可能是我答案的间接解决方案