Not able to download images from google`

Viewed 33

Below is the code to download images from google. When I run the code below, only 80 images are downloaded.

import os
import requests
from bs4 import BeautifulSoup

GOOGLE_IMAGE = 'https://www.google.com/search?site=&tbm=isch&source=hp&biw=1873&bih=990&'

# PATH = r'C:\Program Files\edge\msedgedriver.exe'

usr_agent = {

'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) '
              'Chrome/89.0.4389.90 Safari/537.36 Edg/89.0.774.63',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Encoding': 'none',
'Accept-Language': 'en-US,en;q=0.8',
'Connection': 'keep-alive',
}

SAVE_FOLDER = 'C:\\Users\\sansh\\Desktop\\Py\scrape\\new'

def main():
    if not os.path.exists(SAVE_FOLDER):
        os.mkdir(SAVE_FOLDER)
    download_images()


def download_images():
    data = input('What are u searching for?')
    n_images = int(input('How many images do you want?'))

    print('Start searching ...')

    search_url = GOOGLE_IMAGE + 'q=' + data.replace('_', '+')
    print(search_url)

    response = requests.get(search_url, headers=usr_agent)
    soup = BeautifulSoup(response.text, 'html.parser')

    results = soup.find_all('img', {'class': 'rg_i Q4LuWd'}, limit=n_images)

    image_links = []
    for res in results:
        try:
            link = res['data-src']
            image_links.append(link)

        except KeyError:
            continue

    print(f'Found {len(image_links)} images')

    print("Starting downloader ...")

    for i, image_link in enumerate(image_links):
        down = requests.get(image_link)

        image_name = SAVE_FOLDER + '/' + data + str(i + 1) + '.jpg'
        with open(image_name, 'wb') as file:
            file.write(down.content)

    print("Download completed ...")


if __name__ == '__main__':
    main()

I am not sure what the error is. Also, no error is shown. If possible, can anyone help me solve this problem?

Here, is the screenshot of the result after running this code.

[screenshot][1]
0 Answers
Related