Advertisement
Guest User

Untitled

a guest
Mar 13th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.37 KB | None | 0 0
  1. from selenium import webdriver
  2. from bs4 import BeautifulSoup as bs
  3. import requests
  4. import time
  5. import os
  6.  
  7. users_url = str(raw_input('Enter URL:'))
  8. images = []
  9.  
  10. #This function will get the address of website and scroll it automatically
  11. def get_page():
  12.     global driver
  13.     driver = webdriver.Chrome()
  14.     driver.get(users_url)
  15.     last_height = driver.execute_script('return document.body.scrollHeight')
  16.  
  17.     while True:
  18.         driver.execute_script('window.scrollTo(0, document.body.scrollHeight);')
  19.         time.sleep(1)
  20.         new_height = driver.execute_script('return document.body.scrollHeight')
  21.         if new_height == last_height:
  22.             break
  23.         else:
  24.             last_height = new_height
  25. get_page()
  26.  
  27.  
  28. def get_img():
  29.  
  30.     sp = bs(driver.page_source, 'html.parser')
  31.     for image in sp.find_all('img'):
  32.         images.append(image)
  33. get_img()
  34.  
  35. def make_dir():
  36.     if not os.path.exists('Downloaded images'):
  37.         os.mkdir('Downloaded images')
  38.     os.chdir('Downloaded images')
  39. make_dir()
  40.  
  41. def save_img():
  42.  
  43.     x = 0
  44.  
  45.     for image in images:
  46.         try:
  47.             url = image['src']
  48.             source = requests.get(url)
  49.             with open('img-{}.jpg'.format(x), 'wb') as f:
  50.                 f.write(requests.get(url).content)
  51.                 x += 1
  52.         except:
  53.             print 'Error while saving image.'
  54. save_img()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement