Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

search google images python

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 :: concatenate dataframes 
Python :: combine two dictionary adding values for common keys 
Python :: count lines in file python 
Python :: module installed but not found python 
Python :: how to say something python 
Python :: when was python developed 
Python :: flatten nested list 
Python :: own labels for ticks matplotlib 
Python :: python calculate angle between two points 
Python :: python divisors 
Python :: df.select_dtypes 
Python :: python list empty 
Python :: plot.barh() group by 
Python :: how to select python 3 interpreter in linux 
Python :: extend a class python 
Python :: mutable and immutable in python 
Python :: The specified file cannot be played on the specified MCI device. The file may be corrupt, not in the correct format, or no file handler available for this format. python 
Python :: plt opacity hist 
Python :: how to set breakpoint in python pdb 
Python :: python run command and read output 
Python :: numpy item size 
Python :: difference between object and class in python 
Python :: numpy roundup to nearest 5 
Python :: python Program to check if a given year is leap year 
Python :: list sort by key python 
Python :: get sum from x to y in python 
Python :: python recursive sum of digit 
Python :: python merge two dictionaries in a single expression 
Python :: dense rank in pandas 
Python :: python print color 
ADD CONTENT
Topic
Content
Source link
Name
7+3 =