0

我正在使用 Python 开发一个应用程序来解析网页,然后下载网页上包含的图像。我将 WAMP 用于网络服务器,将 DJango 用于框架。我实现的 python 脚本在我的本地计算机上按预期运行(将图像正确下载到我的本地桌面),但是当我尝试使用 DJango 和 WAMP 在网络服务器上运行它时,我收到错误 [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)
4

1 回答 1

1

您的用户似乎没有“图像”目录的写入权限。将目录设置为“世界可写”,然后重试。

于 2013-05-15T13:56:40.127 回答