Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
尝试使用Python下载文件时出错_Python_Python Requests_Urllib2_Mechanize_Urllib - Fatal编程技术网

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

尝试使用Python下载文件时出错,python,python-requests,urllib2,mechanize,urllib,Python,Python Requests,Urllib2,Mechanize,Urllib,我一直在尝试下载一个链接,以自动股票市场和我的代码运行,但zip文件没有得到下载 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.

我一直在尝试下载一个链接,以自动股票市场和我的代码运行,但zip文件没有得到下载

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:\Users\User\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)

不,请给出一个错误,没有像code3.zip这样的文件或目录。但请欣赏help@arnav你能再试一次吗。我删除了
b
标志。你能在问题中包含完整的错误回溯吗?我在linux上,所以我不能测试它,但我试图做出一个准确的猜测。我只是尝试了一个不同的答案,它奏效了。我想我只是想在下载文件夹中找到它,但是它被下载到了工作目录中。哈哈,那太愚蠢了:P