2016-10-01 119 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(..., ...)