Python-随机Flickr

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python-随机Flickr相关的知识,希望对你有一定的参考价值。

  1. import os
  2. import random
  3. import re
  4. import urllib
  5. import urllib2
  6.  
  7. class flickrImages(object):
  8.  
  9. RE_IMAGEURL = re.compile('src="(http://static.flickr.com/.+?_t.jpg)"', re.DOTALL | re.IGNORECASE)
  10.  
  11. def __init__(self):
  12.  
  13. self.imagesURLs = {}
  14.  
  15. def getRandomImages(self):
  16. '''
  17. Scarica dal sito FlickrImages delle immagini in maniera random...
  18. '''
  19.  
  20. htmlPage = ''
  21. request = ''
  22.  
  23. requestURL = 'http://flickr.com/photos?start=%d' % (random.randint(0, 5000))
  24. requestHeaders = {'User-Agent':'flickrImages/1.0'}
  25.  
  26. try:
  27. request = urllib2.Request(requestURL, None, requestHeaders)
  28. htmlPage = urllib2.urlopen(request).read(500000)
  29. except:
  30. pass
  31.  
  32. results = flickrImages.RE_IMAGEURL.findall(htmlPage)
  33.  
  34. if len(results) > 0:
  35. for image in results:
  36. imageURL = urllib.unquote_plus(image)
  37. if not imageURL.startswith('http://'): imageURL = 'http://'+imageURL
  38. imageURL = imageURL.replace('_t.jpg', '_o.jpg') # Prende il formato piu' grande
  39. self.imagesURLs[imageURL] = 0
  40.  
  41. def downloadImages(self):
  42. '''
  43. Scarica nella cartella googleIMGs le foto che vengono trovate in rete...
  44. '''
  45.  
  46. numberIMGs = len(self.imagesURLs)
  47. posIMGs = 1
  48.  
  49. for imageName in self.imagesURLs:
  50. print '[' + str(posIMGs) + '/' + str(numberIMGs) + '] - ' + imageName
  51. urllib.urlretrieve(imageName, 'flickrIMGs' + os.sep + os.path.split(imageName)[1])
  52. posIMGs += 1
  53.  
  54. if __name__ == '__main__':
  55.  
  56. test = flickrImages()
  57.  
  58. test.getRandomImages()
  59. test.downloadImages()
  60.  
  61. print 'Finito...'

以上是关于Python-随机Flickr的主要内容,如果未能解决你的问题,请参考以下文章

如何用python生成随机的15行6列的随机数据

【Python 】性能优化系列:随机数

python用啥函数产生随机数

Python语言基础——Python 随机模块

python生成随机数随机字符串

python生成随机数随机字符串