/ Published in: Python
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
import os import random import re import urllib import urllib2 class flickrImages(object): RE_IMAGEURL = re.compile('src="(http://static.flickr.com/.+?_t.jpg)"', re.DOTALL | re.IGNORECASE) def __init__(self): self.imagesURLs = {} def getRandomImages(self): ''' Scarica dal sito FlickrImages delle immagini in maniera random... ''' htmlPage = '' request = '' requestURL = 'http://flickr.com/photos?start=%d' % (random.randint(0, 5000)) requestHeaders = {'User-Agent':'flickrImages/1.0'} try: request = urllib2.Request(requestURL, None, requestHeaders) htmlPage = urllib2.urlopen(request).read(500000) except: pass results = flickrImages.RE_IMAGEURL.findall(htmlPage) if len(results) > 0: for image in results: imageURL = urllib.unquote_plus(image) if not imageURL.startswith('http://'): imageURL = 'http://'+imageURL imageURL = imageURL.replace('_t.jpg', '_o.jpg') # Prende il formato piu' grande self.imagesURLs[imageURL] = 0 def downloadImages(self): ''' Scarica nella cartella googleIMGs le foto che vengono trovate in rete... ''' numberIMGs = len(self.imagesURLs) posIMGs = 1 for imageName in self.imagesURLs: print '[' + str(posIMGs) + '/' + str(numberIMGs) + '] - ' + imageName urllib.urlretrieve(imageName, 'flickrIMGs' + os.sep + os.path.split(imageName)[1]) posIMGs += 1 if __name__ == '__main__': test = flickrImages() test.getRandomImages() test.downloadImages() print 'Finito...'