Django给予[Errno 13]权限被拒绝

2024-04-26 03:31:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用Python开发一个应用程序,它解析一个网页,然后下载网页上包含的图像。我使用WAMP作为web服务器,DJango用于框架。我实现的python脚本在本地计算机上按预期运行(将图像正确地下载到本地桌面),但是当我尝试使用DJango和WAMP在web服务器上运行它时,我得到错误[Errno 13]Permission denied:'C:\Users\user123\desktop\images'。下面是我的代码,你知道是什么导致了这个错误。在

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)

Tags: topathinfromimageimportsrcurl