Django给[Errno 13]许可被拒绝

时间:2013-05-15 13:39:04

标签: python django wamp

我正在使用Python开发一个解析网页的应用程序,然后下载网页上包含的图像。我正在为网络服务器使用WAMP,为框架使用DJango。我实现的python脚本在我的本地计算机上按预期运行(正确地将图像下载到我的本地桌面),但是当我尝试使用DJango和WAMP在web服务器上运行它时,我收到错误[Errno 13]权限被拒绝: 'C:\用户\ user123 \桌面\图像'。以下是我的代码,任何想法导致错误。

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 个答案:

答案 0 :(得分:1)

您的用户似乎没有“images”目录的写权限。 将目录设置为“world writeable”,然后重试。