Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

selenium save page as image

from selenium import webdriver
import time, requests

def search_google(search_query):
    browser = webdriver.Chrome()
    search_url = f"https://www.google.com/search?site=&tbm=isch&source=hp&biw=1873&bih=990&q={search_query}"
    images_url = []

    # open browser and begin search
    browser.get(search_url)
    elements = browser.find_elements_by_class_name('rg_i')

    count = 0
    for e in elements:
        # get images source url
        e.click()
        time.sleep(1)
        element = browser.find_elements_by_class_name('v4dQwb')

        # Google image web site logic
        if count == 0:
            big_img = element[0].find_element_by_class_name('n3VNCb')
        else:
           big_img = element[1].find_element_by_class_name('n3VNCb')

        images_url.append(big_img.get_attribute("src"))

        # write image to file
        reponse = requests.get(images_url[count])
        if reponse.status_code == 200:
            with open(f"search{count+1}.jpg","wb") as file:
                file.write(reponse.content)

        count += 1

        # Stop get and save after 5
        if count == 5:
            break

    return images_url

items = search_google('dog')
Comment

PREVIOUS NEXT
Code Example
Python :: python list deep copy 
Python :: numpy reshape 
Python :: how to change value of categorical variable in python 
Python :: python check if number in string 
Python :: how to split string by list of indexes python 
Python :: python add 1 to 100 
Python :: drop 0 in np array 
Python :: how to check if character in string python 
Python :: convert list of lists to pandas dataframe 
Python :: flat numpy array 
Python :: pandas divide multiple columns by one column 
Python :: django choicefield empty label 
Python :: python tkinter messagebox 
Python :: how to find a prime number 
Python :: df split into train, validation, test 
Python :: save pillow image to database django 
Python :: default values python 
Python :: username python 
Python :: pandas como quitar comillas simples de una columna 
Python :: unsplash python 
Python :: print multiplication table python 
Python :: python get line of exception 
Python :: digital differential analyzer 
Python :: make sure it only has letters and numbers python 
Python :: using Decorators 
Python :: sort dict 
Python :: boto3.client python 
Python :: find highest value in array python 
Python :: flask app with spark 
Python :: logical operators python 
ADD CONTENT
Topic
Content
Source link
Name
7+1 =