• 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏吧

尝试使用Python下载文件时出错

python 来源:arnav 3次浏览

我一直在尝试下载一个链接来自动化股票市场,我的代码运行但zip文件没有下载。尝试使用Python下载文件时出错

import urllib 
import urllib2 
import requests 

url ='https://www.nseindia.com/content/historical/EQUITIES/2016/DEC/cm29DEC2016bhav.csv.zip' 
hdr = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11', 
     'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 
     'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3', 
     'Accept-Encoding': 'none', 
     'Accept-Language': 'en-US,en;q=0.8', 
     'Connection': 'keep-alive'} 
print "downloading with urllib" 
urllib.urlretrieve(url, "code.zip") 

print "downloading with urllib2" 
req = urllib2.Request(url, headers=hdr) 
f = urllib2.urlopen(req) 
data = f.read() 
with open("code2.zip", "wb") as code: 
    code.write(data) 

print "downloading with requests" 
r = requests.get(url) 
with open("code3.zip", "wb") as code: 
    code.write(r.content) 

我想zip文件到C下载:\用户\用户\ Downloads文件夹,这样我可以自动解压缩过程,然后将该CSV文件保存到硬盘。任何帮助,将不胜感激。谢谢。

===========解决方案如下:

该文件正在下载到当前工作目录,可能是C:\Python[Version]。去那里检查。试试这个下载到Downloads文件夹:

import requests 

url ='https://www.nseindia.com/content/historical/EQUITIES/2016/DEC/cm29DEC2016bhav.csv.zip' 
print "downloading with requests" 
r = requests.get(url) 
with open("C:\Users\User\Downloads\code3.zip", "w") as code: 
    code.write(r.content) 

版权声明:本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系管理员进行删除。
喜欢 (0)