2016-10-01 121 views
-1

我想从服务器下载zip文件,我还添加了用户代理,以便我可以访问特定文件。但仍然无法访问它。 在执行我的计划,我得到这样的输出:无法访问Python中的url资源

<HTML><HEAD> 
<TITLE>Access Denied</TITLE> 
</HEAD><BODY> 
<H1>Access Denied</H1> 

You don't have permission to access "http...." 

</BODY> 
</HTML> 

源码:

import urllib2 

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

localZipFilePath="Users/SONY/Desktop/cm29SEP2016bhav2.csv.zip" 

hdr = {"User-agent":"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.80 Safari/537.36"} 

webRequest = urllib2.Request(urlOfFileName,headers=hdr) 
page = urllib2.urlopen(webRequest) 
content = page.read() 
+0

尝试添加'接受:*/*'头请求头。 – mhawke

回答

2

在你的代码稍加修改。
从模块urllib使用urlretrieve下载的文件:

import urllib 

urlOfFileName = ... 
localZipFilePath = ... 

# This will directly download the file 
urllib.urlretieve(urlOfFileName, localZipFilePath) 

为PY3使用:

import urllib.request 
urllib.request.urlretrieve(..., ...)