It's really crap, but it "works" for me, 0xc0000022l anwer
import mechanize, os from BeautifulSoup import BeautifulSoup import urllib2
def DownloadIMGs(url): # IMPORTANT URL WITH HTTP OR HTTPS print "From", url dir = 'F:\Downloadss' #Dir for Downloads basicImgFileTypes = ['png','bmp','cur','ico','gif','jpg','jpeg','psd','raw','tif'] browser = mechanize.Browser() html = browser.open(url) soup = BeautifulSoup(html) image_tags = soup.findAll('img') print "N Images:", len(image_tags) print #---------SAVE PATH #check if available if not os.path.exists(dir): os.makedirs(dir) #---------SAVE PATH for image in image_tags: #---------SAVE PATH + FILENAME (Where It is downloading) filename = image['src'] fileExt = filename.split('.')[-1] fileExt = fileExt[0:3] if (fileExt in basicImgFileTypes): print 'File Extension:', fileExt filename = filename.replace('?', '_') filename = os.path.join(dir, filename.split('/')[-1]) num = filename.find(fileExt) + len(fileExt) filename = filename[:num] else: filename = filename.replace('?', '_') filename = os.path.join(dir, filename.split('/')[-1]) + '.' + basicImgFileTypes[0] print 'File Saving:', filename #---------SAVE PATH + FILENAME (Where It is downloading) #--------- FULL URL PATH OF THE IMG imageUrl = image['src'] print 'IMAGE SRC:', imageUrl if (imageUrl.find('http://') > -1 or imageUrl.find('https://') > -1): pass else: if (url.find('http://') > -1): imageUrl = url[:len('http://')] imageUrl = 'http://' + imageUrl.split('/')[0] + image['src'] elif(url.find('https://') > -1): imageUrl = url[:len('https://')] imageUrl = 'https://' + imageUrl.split('/')[0] + image['src'] else: imageUrl = image['src'] print 'IMAGE URL:', imageUrl #--------- FULL URL PATH OF THE IMG #--------- TRY DOWNLOAD try: browser.retrieve(imageUrl, filename) print "Downloaded:", image['src'].split('/')[-1] print except (mechanize.HTTPError,mechanize.URLError) as e: print "Can't Download:", image['src'].split('/')[-1] print pass #--------- TRY DOWNLOAD browser.close() DownloadIMGs('https://stackoverflow.com/questions/15593925/downloading-a-image-using-python-mechanize')
source share