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

我一直在尝试下载一个链接来自动化股票市场,我的代码运行但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)

以上是 尝试使用Python下载文件时出错 的全部内容, 来源链接: utcz.com/qa/261700.html

回到顶部