Python: - 关于从url下载文件的问题

时间:2016-06-14 05:10:30

标签: python

我无法从网站www.nse.com下载文件...当我尝试运行代码时,它会显示以下消息

错误: -          拒绝访问          

拒绝访问

You don't have permission to access "http&#58;&#47;&#47;www&#46;nseindia&#46;com&#47;content&#47;historical&#47;EQUITIES&#47;2016&#47;JUN&#47;cm13JUN2016bhav&#46;csv&#46;zip" on this server.<P>
Reference&#32;&#35;18&#46;4ef0ef75&#46;1465880369&#46;ab8f79e
</BODY>
</HTML>

未下载

我的代码就是这样:

CODE

import urllib2

urlOfFileName="http://www.nseindia.com/content/historical/EQUITIES/2016/JUN/cm13JUN2016bhav.csv.zip"

localZipFilePath="C:\Users\sai\Desktop\cm13JUN2016bhav.csv.zip"

hdr ={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.84 Safari/537.36'}

webRequest=urllib2.Request(urlOfFileName,headers=hdr)

try:
    page=urllib2.urlopen(webRequest)
    content=page.read()
    output=open(localZipFilePath,"wb")
    output.write(bytearray(content))
    output.close()

except urllib2.HTTPError,e:
    print e.fp.read()
    print "Not Downloaded"

我也使用了User_Agent Header ..我无法理解这个问题。请帮我解决这个问题。

0 个答案:

没有答案