daily pastebin goal
94%
SHARE
TWEET

Untitled

a guest Apr 17th, 2018 52 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import requests
  2. from bs4 import BeautifulSoup as bs
  3. import os
  4.  
  5. # website with model images
  6. url = 'https://www.pexels.com/search/new%20york/'
  7.  
  8. # download page for parsing
  9. page = requests.get(url)
  10. soup = bs(page.text, 'html.parser')
  11.  
  12. # locate all elements with image tag
  13. image_tags = soup.findAll('img')
  14.  
  15. # create directory for model images
  16. if not os.path.exists('models'):
  17.     os.makedirs('models')
  18.  
  19. # move to new directory
  20. os.chdir('models')
  21.  
  22. # image file name variable
  23. x = 0
  24.  
  25. # writing images
  26. for image in image_tags:
  27.     try:
  28.         url = image['src']
  29.         response = requests.get(url)
  30.         if response.status_code == 200:
  31.             with open('model-' + str(x) + '.jpg', 'wb') as f:
  32.                 f.write(requests.get(url).content)
  33.                 f.close()
  34.                 x += 1
  35.     except:
  36.         pass
RAW Paste Data
Top