Web ページを解析し、Web ページに含まれる画像をダウンロードする Python を使用したアプリケーションを開発しています。Web サーバーには WAMP を、フレームワークには 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)