2013-05-15 170 views
0

我正在開發一個應用程序,它解析一個網頁,然後下載網頁上的圖像。我正在使用WAMP作爲框架的網絡服務器和DJango。在我的本地計算機上,我實現的python腳本按預期運行(將圖像正確下載到本地桌面),但是當我嘗試在使用DJango和WAMP的web服務器上運行它時,出現錯誤[Errno 13] Permission denied: 'C:\用戶\ user123 \桌面\圖像'。下面是我的代碼,任何想法是什麼導致了錯誤。Django給予[Errno 13]權限被拒絕

from django.http import HttpResponse 
from bs4 import BeautifulSoup as bsoup 
import urlparse 
from urllib2 import urlopen 
from urllib import urlretrieve 
import os 
import sys 
import zipfile 
from django.core.servers.basehttp import FileWrapper 

def getdata(request): 
out = r'C:\Users\user123\Desktop\images' 
if request.GET.get('q'): 
    #url = str(request.GET['q']) 
    url = "http://google.com" 
    soup = bsoup(urlopen(url)) 
    parsedURL = list(urlparse.urlparse(url)) 

    for image in soup.findAll("img"): 
     print "Old Image Path: %(src)s" % image 
    #Get file name 
    filename = image["src"].split("/")[-1] 
    #Get full path name if url has to be parsed 
    parsedURL[2] = image["src"] 
    image["src"] = '%s\%s' % (out,filename) 
    print 'New Path: %s' % image["src"] 
    #  print image 
    outpath = os.path.join(out, filename) 

    # 
    if image["src"].lower().startswith("http"): 
     urlretrieve(image["src"], outpath) 
    else: 
     urlretrieve(urlparse.urlunparse(parsedURL), out) #Constructs URL from tuple (parsedURL) 

    #Create HTML File and writes to it to check output (stored in same directory). 
    html = soup.prettify("utf-8") 
    with FileWrapper(open("output.html", "wb")) as file: 
     file.write(html) 

    #Create where zip file will be stored (same directory htmlparser file) 
    zip = zipfile.ZipFile('C:\Users\user123\Desktop\Images.zip', 'w') 

    #Path where file that will be zipped up is located 
    path = 'images' 

    #For each file, add it to the zip folder. 
    for root, dirs, files in os.walk(path): 
     for file in files: 
      zip.write(os.path.join(root, file)) 
    zip.close() 
else: 
     url = 'You submitted nothing!' 

return HttpResponse(url) 

回答

1

您的用戶似乎沒有「圖像」目錄的寫入權限。 將目錄設置爲「世界可寫」,然後重試。

+1

感謝mawimawi的迴應,這是我在WAMP/Django環境中做的事情,還是我在我正在使用的Windows桌面上單獨設置文件夾權限? – johns4ta

+1

我的猜測是Windows桌面上的文件夾權限。但是因爲我從來沒有部署到Windows(也沒有在Windows中編碼),所以我無法確定。 – mawimawi

+0

我爲每個人設置了文件夾權限,但沒有解決問題。我在網上找到了一些線索,我會看看它們是否會導致任何問題。再次感謝! – johns4ta

相關問題