python - Unable to open file object created in Django -
i'm developing application runs on apache server django framework. current script works fine when runs on local desktop (without django). script downloads images website folder on desktop. however, when run script on server file object create django apparently has in (should google's logo), however, can't open file. create html file, updated image link locations, html file gets created fine, i'm assuming because it's text, maybe? believe may have use file wrapper somewhere, i'm not sure. appreciated, below code, thanks!
from django.http import httpresponse bs4 import beautifulsoup bsoup import urlparse urllib2 import urlopen urllib import urlretrieve import os import sys import zipfile django.core.servers.basehttp import filewrapper def getdata(request): out = 'c:\users\user\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)) 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 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) #retrieve images if image["src"].lower().startswith("http"): urlretrieve(image["src"], outpath) else: urlretrieve(urlparse.urlunparse(parsedurl), out) #constructs url tuple (parsedurl) #create html file , writes check output (stored in same directory). html = soup.prettify("utf-8") open("output.html", "wb") file: file.write(html) else: url = 'you submitted nothing!' return httpresponse(url)
my problem had storing files on desktop. stored files in django workspace folder, changed paths, , worked me.
Comments
Post a Comment