Advertisement
remiliod

Untitled

Dec 16th, 2017
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.66 KB | None | 0 0
  1. #imgur.py lets you doenload all the pictures inside a recent gallery from a
  2. #         certain category in imgur
  3. #         usage:   imgur.py <category>  example: science
  4.  
  5.  
  6.  
  7.  
  8. import os, requests, bs4, sys
  9.  
  10.  
  11. if len(sys.argv)>1:
  12.     category = sys.argv[1]
  13.     os.chdir('C:\\Python36\\Scripts')
  14.     url = 'https://imgur.com/search?q=' + category
  15.     print(url)
  16.     os.makedirs(category, exist_ok=True)
  17.  
  18.     res = requests.get(url)
  19.     res.raise_for_status()
  20.  
  21.     soup = bs4.BeautifulSoup(res.text, "html.parser")
  22.     fotos = soup.select('a[class="image-list-link"]')
  23.  
  24.     for i in range(10):                                        #change the range to get into more Galleries
  25.         #URL de la galleria
  26.         basename = fotos[i].get('href')
  27.         linkToGallery = 'https://imgur.com'+basename
  28.         res2 = requests.get(linkToGallery)
  29.         res2.raise_for_status()
  30.         print('Desde la galleria %s' %linkToGallery)
  31.  
  32.         soup2 = bs4.BeautifulSoup(res2.text, "html.parser")
  33.  
  34.         #imagenes dentro de la galleria
  35.         fotosInside = soup2.select('a img')
  36.         if fotosInside == []:
  37.             print('x x x No image found in this gallery')
  38.         for i in range(len(fotosInside)):
  39.             #link individual de cada foto
  40.             linkInside = 'http:'+ fotosInside[i].get('src')
  41.  
  42.             res3 = requests.get(linkInside)
  43.             res3.raise_for_status()
  44.             imageFile = open(os.path.join(category, os.path.basename(linkInside)),'wb')
  45.             print('- - - Descargando la imagen %s' %linkInside)
  46.             for chunk in res3.iter_content(100000):
  47.                 imageFile.write(chunk)
  48.             imageFile.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement