Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

download images python google

from selenium import webdriver
from bs4 import BeautifulSoup
import requests
import urllib.request
import time
import sys
import os


#taking user input
print("What do you want to download?")
download = input()
site = 'https://www.google.com/search?tbm=isch&q='+download


#providing driver path
driver = webdriver.Firefox(executable_path = 'C:Driversgeckodriver.exe')

#passing site url
driver.get(site)


#if you just want to download 10-15 images then skip the while loop and just write
#driver.execute_script("window.scrollBy(0,document.body.scrollHeight)")


#below while loop scrolls the webpage 7 times(if available)

i = 0

while i<7:  
	#for scrolling page
    driver.execute_script("window.scrollBy(0,document.body.scrollHeight)")
    
    try:
		#for clicking show more results button
        driver.find_element_by_xpath("/html/body/div[2]/c-wiz/div[3]/div[1]/div/div/div/div/div[5]/input").click()
    except Exception as e:
        pass
    time.sleep(5)
    i+=1

#parsing
soup = BeautifulSoup(driver.page_source, 'html.parser')


#closing web browser
driver.close()


#scraping image urls with the help of image tag and class used for images
img_tags = soup.find_all("img", class_="rg_i")


count = 0
for i in img_tags:
    #print(i['src'])
    try:
		#passing image urls one by one and downloading
        urllib.request.urlretrieve(i['src'], str(count)+".jpg")
        count+=1
        print("Number of images downloaded = "+str(count),end='
')
    except Exception as e:
        pass
Comment

PREVIOUS NEXT
Code Example
Python :: change image resolution pillow 
Python :: count values in numpy list python 
Python :: password generator in python 
Python :: addition in python 
Python :: python break long string multiple lines 
Python :: python - count values that contain special characters 
Python :: capitalise words in a column pandas 
Python :: python range of letters 
Python :: python correlation between features and target 
Python :: how to print a specific value in a list python 
Python :: print only numbers from string python 
Python :: python with file 
Python :: geometrical mean python 
Python :: add text to plot python scatter 
Python :: TypeError: cannot unpack non-iterable int object 
Python :: word generator in python 
Python :: playsound error 
Python :: clear text box tkinter python 
Python :: how to make a pause in python 
Python :: python debugger 
Python :: How to Get the Difference Between Sets in Python 
Python :: python bool to string 
Python :: change colorbar size and place python 
Python :: python reverse a string 
Python :: feature scaling in python 
Python :: get sum in range 
Python :: python adding digits 
Python :: merge two dictionaries in a single expression 
Python :: python check if all caps 
Python :: python wait for x seconds 
ADD CONTENT
Topic
Content
Source link
Name
9+3 =