Scrape and Download Google Images with Python

The increasing prevalence of web scraping has increased its usage areas considerably. Many AI applications today feed web scraping datasets with up-to-date data on a regular basis. There is a regular and uninterrupted flow of data from target websites to the data sets of artificial intelligence applications.

Related Post: The Impact of Artificial Intelligence on the Future of Customer Success

Image processing is one of the most popular areas in artificial intelligence applications. Image processing is a field of computer science that focuses on enabling computers to identify and understand objects and people in images and videos. Like other types of artificial intelligence, image processing aims to perform and automate tasks that replicate human capabilities. In this case, image processing tries to copy both the way people see and the way they make sense of what they see.

The data required for the development of algorithms and reduction of error margins in many fields, especially in image processing projects, is obtained by web scraping. In this article, we will develop an application that is frequently used in image processing projects. We will scrape and download Google Images with the Python programming language. So let’s get started.

Project Setup

First, let’s open a folder on the desktop. Let’s open a terminal in this file path and install the necessary libraries by running the command below.

pip install requests bs4

After installing the necessary libraries, let’s create a file named ‘index.py’ in the folder.

Also Read: The ChatGPT – Its Implications for the Businesses

Code

Let’s paste the following codes into the ‘index.py’ file we created.

import requests, re, json, urllib.request
from bs4 import BeautifulSoup

headers = {
“User-Agent”: “Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.5060.114 Safari/537.36”
}

queryParameters = {“q”: “marvel”, “tbm”: “isch”, “hl”: “en”, “gl”: “us”, “ijn”: “0” }

target_image_path = “.isv-r.PNCib.MSM1fd.BUooTd”
targetted_image_base_html_path = “.VFACy.kGQAp.sMi44c.lNHeqe.WGvvNb”

html = requests.get(“https://ift.tt/7ndzuiw;, params=queryParameters, headers=headers, timeout=30)
soup = BeautifulSoup(html.text, “lxml”)

google_images = []

def scrape_and_download_google_images():

images_data_in_json = convert_image_to_json()

matched_image_data = re.findall(r’\”b-GRID_STATE0\”(.*)sideChannel:\s?{}}’, images_data_in_json)

removed_matched_thumbnails = remove_matched_get_thumbnails(matched_google_image_data=matched_image_data)

matched_resolution_images = re.findall(r”(?:’|,),\[\”(https:|http.*?)\”,\d+,\d+\]”, removed_matched_thumbnails)

full_resolution_images = get_resolution_image(matched_resolution_images=matched_resolution_images)

for index, (image_data, image_link) in enumerate(zip(soup.select(target_image_path), full_resolution_images), start=1):

append_image_to_list(image_data=image_data, image_link=image_link)

print(f'{index}. image started to download’)

download_image(image_link=image_link, index=index)

print(f'{index}. image successfully downloaded’)

print(f’scraped and downloaded images: {google_images}’)

def remove_matched_get_thumbnails(matched_google_image_data):
return re.sub(
r’\[\”(https\:\/\/encrypted-tbn0\.gstatic\.com\/images\?.*?)\”,\d+,\d+\]’, “”, str(matched_google_image_data))

def get_resolution_image(matched_resolution_images):
return [
bytes(bytes(img, “ascii”).decode(“unicode-escape”), “ascii”).decode(“unicode-escape”) for img in matched_resolution_images
]

def convert_image_to_json():
all_script_tags = soup.select(“script”)
images_data = “”.join(re.findall(r”AF_initDataCallback\(([^<]+)\);”, str(all_script_tags)))
fixed_images_data = json.dumps(images_data)
return json.loads(fixed_images_data)

def append_image_to_list(image_data, image_link):
google_images.append({
“image_title”: image_data.select_one(targetted_image_base_html_path)[“title”],
“image_source_link”: image_data.select_one(targetted_image_base_html_path)[“href”],
“image_link”: image_link
})

def download_image(image_link, index):
opener=urllib.request.build_opener()
opener.addheaders=[(‘User-Agent’,’Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.54 Safari/537.36′)]
urllib.request.install_opener(opener)
urllib.request.urlretrieve(image_link, f’Scraped_Images/Image_{index}.jpg’)

scrape_and_download_google_images()

 

If we examine the codes, let’s first look at the fields with static values. The following fields are defined as static values. We specify the name and properties of the image we want to scrape and download with the queryParams variable.

headers = {
“User-Agent”: “Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.5060.114 Safari/537.36”
}

queryParameters = {“q”: “marvel”, “tbm”: “isch”, “hl”: “en”, “gl”: “us”, “ijn”: “0” }

target_image_path = “.isv-r.PNCib.MSM1fd.BUooTd”
targeted_image_base_html_path = “.VFACy.kGQAp.sMi44c.lNHeqe.WGvvNb”

html = requests.get(“https://ift.tt/7ndzuiw;, params=queryParameters, headers=headers, timeout=30)
soup = BeautifulSoup(html.text, “lxml”)

google_images = []

The scrape_and_download_google_images() method is where the stream starts. targeted images are scraped and then downloaded to the folder we specified.

def scrape_and_download_google_images():

images_data_in_json = convert_image_to_json()

matched_image_data = re.findall(r’\”b-GRID_STATE0\”(.*)sideChannel:\s?{}}’, images_data_in_json)

removed_matched_thumbnails = remove_matched_get_thumbnails(matched_google_image_data=matched_image_data)

matched_resolution_images = re.findall(r”(?:’|,),\[\”(https:|http.*?)\”,\d+,\d+\]”, removed_matched_thumbnails)

full_resolution_images = get_resolution_image(matched_resolution_images=matched_resolution_images)

for index, (image_data, image_link) in enumerate(zip(soup.select(target_image_path), full_resolution_images), start=1):

append_image_to_list(image_data=image_data, image_link=image_link)

print(f'{index}. image started to download’)

download_image(image_link=image_link, index=index)

print(f'{index}. image successfully downloaded’)

print(f’scraped and downloaded images: {google_images}’)

Downloading the scraped image to the folder is done in the download_image(image_link=image_link, index=index) method. The image scraped in this method is saved in the “Scraped_Images” that we previously added to the file location of the project.

Note: Create “Scraped_Images” folder in the project structure before running the application

def download_image(image_link, index):
opener=urllib.request.build_opener()
opener.addheaders=[(‘User-Agent’,’Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.54 Safari/537.36′)]
urllib.request.install_opener(opener)
urllib.request.urlretrieve(image_link, f’Scraped_Images/Image_{index}.jpg’)

Also Read:What Are The Applications And Benefits Of Blockchain-as-a-service For Businesses?

Run

To run the application, let’s open a terminal in the file location and run the following command.

python index.py

After the application runs, the following information is printed on the console of the application.

1. image started to download
1. image successfully downloaded
2. image started to download
2. image successfully downloaded
3. image started to download
3. image successfully downloaded
4. image started to download
4. image successfully downloaded
5. image started to download
5. image successfully downloaded
6. image started to download
6. image successfully downloaded
7. image started to download
7. image successfully downloaded
[…]
48. image started to download
48. image successfully downloaded

scraped and downloaded images:
[
{
‘image_title’: ‘Marvel Comics – Wikipedia’,
‘image_source_link’: ‘https://ift.tt/B4IC8LA;,
‘image_link’: ‘https://ift.tt/3qZ9sy4;
},
{
‘image_title’: ‘Marvel Universe – Wikipedia’,
‘image_source_link’: ‘https://ift.tt/9j7Htnf;,
‘image_link’: ‘https://ift.tt/iMEIPzJ;
},
{
‘image_title’: ‘Marvel.com | The Official Site for Marvel Movies, Characters, Comics, TV’,
‘image_source_link’: ‘https://ift.tt/pSCeztu;,
‘image_link’: ‘https://i.annihil.us/u/prod/marvel/images/OpenGraph-TW-1200×630.jpg’
},
{
‘image_title’: ‘Marvel Entertainment – YouTube’,
‘image_source_link’: ‘https://www.youtube.com/c/marvel’,
‘image_link’: ‘https://ift.tt/29JcxGY;
},
{
‘image_title’: ‘Marvel movies and shows | Disney+’,
‘image_source_link’: ‘https://ift.tt/yiIwR3H;,
‘image_link’: ‘https://ift.tt/szlTy0n;
},
{
‘image_title’: ‘An Intro to Marvel for Newbies | WIRED’,
‘image_source_link’: ‘https://ift.tt/QmbIgVX;,
‘image_link’: ‘https://ift.tt/x6sriLj;
},
{
‘image_title’: ‘How to Watch Every Marvel Movie in Order of Story – Parade: Entertainment,  Recipes, Health, Life, Holidays’,
‘image_source_link’: ‘https://ift.tt/BUXKkaY;,
‘image_link’: ‘https://ift.tt/NXOPZYi;
},
{
‘image_title’: ‘Marvel Comics | History, Characters, Facts, & Movies | Britannica’,
‘image_source_link’: ‘https://ift.tt/Hy7J95x;,
‘image_link’: ‘https://ift.tt/qfVybE7;
},
{
‘image_title’: ‘Captain Marvel (2019) – IMDb’,
‘image_source_link’: ‘https://ift.tt/8Pi9b5a;,
‘image_link’: ‘https://ift.tt/Kgm0Pjb;
},
[…]
]

Let’s look at the “Scraped_Images” file to check the downloaded images.

Scraped Images

Also Read: Types Of Corporations And How To Incorporate Your Startup Business

Conclusion

We did the scraping and downloading of Google Images, which was needed for some reason, with python. If you want to get the images you need from Google without writing any code, explore the Zenserp API. Here is its powerful and wonderful documentation that is constantly updated.

The post Scrape and Download Google Images with Python first appeared on Tycoonstory Media.


Post a Comment

0 Comments