|
| 1 | +from selenium import webdriver |
| 2 | +from selenium.webdriver.common.keys import Keys |
| 3 | +from selenium.webdriver.common.by import By |
| 4 | +from selenium.webdriver.support import expected_conditions as EC |
| 5 | +from selenium.webdriver.support.ui import WebDriverWait |
| 6 | +from selenium.webdriver import Chrome |
| 7 | +import time |
| 8 | +import os |
| 9 | +import wget |
| 10 | + |
| 11 | +# Inputs |
| 12 | + |
| 13 | +user = input('Enter your username:') |
| 14 | +passuser = input('Enter your password:') |
| 15 | +try: |
| 16 | + searchName = input('write what you like to search at Instagram : ') |
| 17 | + block = ['$', '@', '#', '(', ')', '=', '-', '{', '}', '?'] |
| 18 | + for i in block: |
| 19 | + if i in searchName: |
| 20 | + print('There is a code that is not allowed ') |
| 21 | + break |
| 22 | +except: |
| 23 | + print('Error, try again') |
| 24 | + searchName = input('write what you like to search at Instagram : ') |
| 25 | + |
| 26 | + |
| 27 | +try: |
| 28 | + ManyImg = int(input('How many imges you want? Enter number please: ')) |
| 29 | +except: |
| 30 | + print('error, please enter numbers. ') |
| 31 | + ManyImg = input(int('How many imges you want? Enter number please: ')) |
| 32 | + |
| 33 | +# Browser |
| 34 | + |
| 35 | +url = 'https://www.instagram.com/' |
| 36 | +driver = webdriver.Chrome() |
| 37 | +driver.get(url) |
| 38 | + |
| 39 | + |
| 40 | +# Login |
| 41 | + |
| 42 | +username = WebDriverWait(driver, 10).until( |
| 43 | + EC.element_to_be_clickable((By.NAME, 'username'))) |
| 44 | +password = WebDriverWait(driver, 10).until( |
| 45 | + EC.element_to_be_clickable((By.NAME, 'password'))) |
| 46 | + |
| 47 | +username.clear() |
| 48 | +time.sleep(1) |
| 49 | +password.clear() |
| 50 | +time.sleep(1) |
| 51 | + |
| 52 | +username.send_keys(user) |
| 53 | +time.sleep(1) |
| 54 | +password.send_keys(passuser) |
| 55 | + |
| 56 | +# Login Bottom |
| 57 | + |
| 58 | +login = WebDriverWait(driver, 10).until(EC.element_to_be_clickable( |
| 59 | + (By.XPATH, '//*[@id="loginForm"]/div/div[3]/button/div'))) |
| 60 | +login.click() |
| 61 | + |
| 62 | +# Close the window |
| 63 | +try: |
| 64 | + NotNow = WebDriverWait(driver, 10).until(EC.element_to_be_clickable( |
| 65 | + (By.XPATH, "//button[contains(text(), 'Not Now')]"))).click() |
| 66 | + time.sleep(3) |
| 67 | + NotNow = WebDriverWait(driver, 10).until(EC.element_to_be_clickable( |
| 68 | + (By.XPATH, "//button[contains(text(), 'Not Now')]"))).click() |
| 69 | +except: |
| 70 | + print('error') |
| 71 | + |
| 72 | + |
| 73 | +# Search |
| 74 | + |
| 75 | +Search = WebDriverWait(driver, 10).until(EC.element_to_be_clickable( |
| 76 | + (By.CSS_SELECTOR, "input[placeholder = 'Search']"))) |
| 77 | +Search.clear() |
| 78 | +time.sleep(2) |
| 79 | +keyword = f'#{searchName}' |
| 80 | +Search.send_keys(keyword) |
| 81 | +Search.send_keys(Keys.ENTER) |
| 82 | +Search.clear() |
| 83 | + |
| 84 | +time.sleep(3) |
| 85 | + |
| 86 | +# loop Enter for search |
| 87 | +loop = True |
| 88 | +while loop: |
| 89 | + if url == driver.current_url: |
| 90 | + Search = WebDriverWait(driver, 10).until(EC.element_to_be_clickable( |
| 91 | + (By.CSS_SELECTOR, "input[placeholder = 'Search']"))) |
| 92 | + Search.send_keys(Keys.ENTER) |
| 93 | + else: |
| 94 | + loop = False |
| 95 | + |
| 96 | +# getting on images |
| 97 | + |
| 98 | +time.sleep(5) |
| 99 | +images = driver.find_elements(By.TAG_NAME, 'img') |
| 100 | +scroll = 0 |
| 101 | +while len(images) < ManyImg: |
| 102 | + scroll += 1500 |
| 103 | + time.sleep(3) |
| 104 | + driver.execute_script(f"window.scrollTo(0, {scroll})") |
| 105 | + time.sleep(5) |
| 106 | + images = driver.find_elements(By.TAG_NAME, 'img') |
| 107 | + |
| 108 | + |
| 109 | +# get src |
| 110 | + |
| 111 | +time.sleep(3) |
| 112 | +images = [image.get_attribute('src') for image in images] |
| 113 | + |
| 114 | +# create Folder |
| 115 | +time.sleep(1) |
| 116 | +path = os.getcwd() |
| 117 | +path = os.path.join(path, keyword[1:] + 's') |
| 118 | +os.mkdir(path) |
| 119 | + |
| 120 | +# filling the folder by images |
| 121 | +counter = 0 |
| 122 | +for image in images: |
| 123 | + save_as = os.path.join(path, keyword[1:] + str(counter) + '.jpg') |
| 124 | + wget.download(image, save_as) |
| 125 | + counter += 1 |
| 126 | + |
| 127 | +print('Finish') |
| 128 | +driver.quit() |
0 commit comments