Python-随机Flickr
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python-随机Flickr相关的知识,希望对你有一定的参考价值。
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...'
以上是关于Python-随机Flickr的主要内容,如果未能解决你的问题,请参考以下文章