<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Dian Octaviani on Medium]]></title>
        <description><![CDATA[Stories by Dian Octaviani on Medium]]></description>
        <link>https://medium.com/@dian.octaviani?source=rss-ef126d40c4b8------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*Issef8qjaNfZ2pU2.jpg</url>
            <title>Stories by Dian Octaviani on Medium</title>
            <link>https://medium.com/@dian.octaviani?source=rss-ef126d40c4b8------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 05 Dec 2026 02:59:46 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@dian.octaviani/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Automation of Google Image Scraping using Selenium]]></title>
            <link>https://medium.com/@dian.octaviani/method-1-4-automation-of-google-image-scraping-using-selenium-3972ea3aa248?source=rss-ef126d40c4b8------2</link>
            <guid isPermaLink="false">https://medium.com/p/3972ea3aa248</guid>
            <category><![CDATA[scraping]]></category>
            <category><![CDATA[selenium]]></category>
            <category><![CDATA[google-images]]></category>
            <category><![CDATA[web-scraping]]></category>
            <category><![CDATA[web-automation]]></category>
            <dc:creator><![CDATA[Dian Octaviani]]></dc:creator>
            <pubDate>Fri, 27 Aug 2021 11:53:50 GMT</pubDate>
            <atom:updated>2022-07-07T08:28:37.118Z</atom:updated>
            <content:encoded><![CDATA[<p>Method of Web Scraping: using <strong>Selenium</strong></p><blockquote>Code is available in: <a href="https://github.com/dianoctaviani/selenium_img_scraper">https://github.com/dianoctaviani/selenium_img_scraper</a></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RUb2yKHOEQori_CxDd53kA.jpeg" /><figcaption><strong>Python + Selenium + Chrome Web Driver</strong></figcaption></figure><h3>Scraping using Selenium</h3><p>This blog post explores the method of scraping Google Images using <strong>Selenium</strong> with a combination of <strong>Python</strong> and <strong>Chrome Web Driver. </strong>Providing a practical example of how Selenium can be applied. This post also aims to explain the basics of Selenium and how it works.</p><h3>Background</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6FoEU9xVC4xsMDSNPeJTbw.png" /><figcaption>Photo by <a href="https://unsplash.com/@juliazolotova?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Julia Zolotova</a> on <a href="https://unsplash.com/?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>For context, we currently have a dataset containing 118 searchable pairs of keywords, consisting of both <strong>common and</strong> <strong>scientific</strong> <strong>botanical names of edible vegetables and fruits. </strong>Using the list of <strong>scientific</strong> names, we need 1 visual representation of each vegetable or fruit to build a knowledge base.</p><blockquote><strong>Note</strong>: The reason we rely on using scientific name instead of the common name is so that we eliminate the possibility of retrieving images based on fruit-based brand names, such as Apple Inc.</blockquote><p>For the list of names, I created another scraping script (<em>not included in this tutorial</em>) to retrieve the list of common and scientific botanical names of edible vegetables and fruits.</p><p>I’ve compiled the dataset into a <strong>.csv</strong> and made it available for download within the input folder in the provided <a href="https://github.com/dianoctaviani/selenium_img_scraper">GitHub repo</a> as well as on <strong>Kaggle</strong>: <a href="https://www.kaggle.com/dianoctaviani/scientific-botanical-names-of-fruits-vegetables">https://www.kaggle.com/dianoctaviani/scientific-botanical-names-of-fruits-vegetables</a></p><blockquote><strong>Note</strong>: This project is for learning and research only. The datasets and images are not reusable for commercialised purposes. Using a web scraper to harvest data off the Internet is not a criminal act on its own. Many times, <strong>it is absolutely legal to scrape a website</strong>, but the way you intend to use that data may be illegal.</blockquote><h3>Basics of Selenium</h3><p><strong>What is Selenium?</strong> Selenium is a tool for controlling web browsers through programs and performing browser automation. It is mainly used as a testing framework for cross-web browser platforms. However, Selenium is also a very capable tool to use for general web automation, as we are able to program it to do what a human user can do on a browser (in this case, to programmatically download images from Google).</p><p>For the automation work, we will be using a combination of <strong>Selenium, Python </strong>and <strong>Chrome Web Driver</strong>.</p><h4>Locator Strategies</h4><p>So <strong>how does Selenium exactly work</strong>? Well, Selenium provides the mechanisms to locate elements on a web page, which can be sequentially put together to mimic a user’s interaction on the web page.</p><p>Selenium has a range of locator strategies, to be precise, there are currently 9 different ways to locate elements using Selenium.</p><pre>1. find_element_by_id - <strong>ID</strong><br>2. find_element_by_name - <strong>Name</strong> <br>3. find_element_by_link_text - <strong>Linktext</strong><br>4. find_element_by_partial_link_text - <strong>Partial Linktext</strong><br>5. find_element_by_tag_name - <strong>Tag Name</strong><br>6. find_element_by_class_name - <strong>Class Name</strong><br>7. find_element_by_css_selector - <strong>CSS (Cascaded Style Sheets)</strong><br>8. find_element_by_xpath - <strong>XPath (XML Path)<br></strong>9. <strong>DOM (Data Object Modelling)</strong></pre><p>These elements can be identified using the <strong>Inspector</strong> feature available from the <strong>Developer Tool</strong> on the browser.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/911/1*8He1rfELG_rCI48fO3l6Zw.png" /><figcaption>Browser Developer Tool</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YUdLXgubuVaMsQFrJ8BZkg.png" /><figcaption>Selected a web element using Inspector</figcaption></figure><p>Once the web element is selected, we can then choose to copy the element as various options, such as <strong>XPath</strong>, <strong>outerHTML</strong>, <strong>JS Path</strong> etc and analyse the relevant id, names, tags or paths that we need to perform the required actions.</p><h4>Headless Browser</h4><p>Before we dive into the code, let’s talk about what a <strong>headless </strong>browser is and why we need to use it. In short, headless browsers are web browsers without a graphical user interface (GUI) and are usually controlled programmatically or via a command-line interface (CLI).</p><p>Automating the driving of Chrome with GUI enabled can increase CPU and/or memory usage. Both are associated with having to display the browser with rendered graphics from the requested URL. This is increased even more when there are multiple windows or tabs launched simultaneously.</p><p>Therefore, headless browsers are useful in terms of eliminating the need to launch GUIs each time the automation script is running.</p><h3>Method of Automation</h3><h4>Setup</h4><blockquote>Code is available in: <a href="https://github.com/dianoctaviani/selenium_img_scraper">https://github.com/dianoctaviani/selenium_img_scraper</a></blockquote><p>Now, let’s set up the necessary libraries required for the script.</p><p>Install <strong>Selenium</strong> using PyPi:</p><pre>$ pip3 install selenium</pre><p>Install the <strong>WebDriver Manager </strong>using PyPi:</p><pre>$ pip3 install webdriver_manager</pre><h4>Sequence of Action</h4><p>To obtain the images of each fruit or vegetable, we will create a script to iterate through the list of names to be searched on Google and then proceed to download the first image that appears from each search result (the images will retain original size and resolution).</p><p>I’ve separated the actions summarised above into two separate <strong>.py</strong> scripts:</p><pre>1. selenium_img_src_crawler.py<br>2. selenium_img_downloader.py</pre><p>This separation is useful to make the script appear less complicated for new starters, and it is also built for better customisation around bulk search input.</p><p>The first script (<a href="https://github.com/dianoctaviani/selenium_img_scraper/blob/main/selenium_img_crawler.py"><strong>selenium_img_src_crawler.py</strong></a>) is used to search and retrieve image source links in bulk and outputs them into <strong>img_src_links.csv</strong>. Whereas the second script will later process the links and perform the bulk downloads.</p><p>Here’s the step by step explanation of what’s included in the first script.</p><ol><li><strong>Importing Essentials: </strong>The following modules are required for the first script.</li></ol><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a0ec6504880e1ff3b2070dbc3871e104/href">https://medium.com/media/a0ec6504880e1ff3b2070dbc3871e104/href</a></iframe><p><strong>2. </strong>Read and store the list of fruits and vegetable names under the <strong>scientific_names</strong> column within a file called <strong>input</strong>/<strong>scientific_botanical_names_veggies_fruits.csv</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/335/1*vqPkEuCpfor4DTmV28zbNA.png" /></figure><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/02bdb13b6d567796aa838fe5303025bb/href">https://medium.com/media/02bdb13b6d567796aa838fe5303025bb/href</a></iframe><blockquote><strong>Note</strong>: The search terms can be customised, just simply modify the files to include a list of search terms and change the column names.</blockquote><p><strong>3. ChromeDriverManager </strong>will install the web driver for chrome and skips it if an existing driver is found in the cache. A function called <strong>search_google </strong>will iteratively search the list of scientific names via Google Images by dynamically assigning the keyword to the <strong>search_url.</strong></p><p>Also within the function, there are several options enabled, such as <strong>disable-gpu</strong> and <strong>headless</strong> browsing.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/637/1*F-ST6HEpK_tEskoaKIdJvA.png" /></figure><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5a9778a2c5cf4d016afb2f0ef01cb161/href">https://medium.com/media/5a9778a2c5cf4d016afb2f0ef01cb161/href</a></iframe><p><strong>4.</strong> Expanding on the same <strong>search_google </strong>function, it has been programmed to click on the first image box that appears from the search result by finding the element through XPath.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*EKqNZbrUnYFuCqmhdUkQgA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/925/1*wQXUt0XCPISpaEIh6bB7jQ.png" /><figcaption>Select the element using inspector &amp; select the Copy XPath option</figcaption></figure><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/dad03697d1ad3aa1df46895939264f0f/href">https://medium.com/media/dad03697d1ad3aa1df46895939264f0f/href</a></iframe><p><strong>5. </strong>The next snippet within <strong>search_google </strong>function retrieves the link of the original image source by getting the attribute containing the source (src).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/888/1*NGihy9B0-pzHB7rEpALJ4g.png" /><figcaption>Base64 encoded img src content</figcaption></figure><p>This is where it gets a bit challenging. By default, within the thumbnail boxes, Google does not store the image src attribute as a plain URL. It is instead stored as <strong>base64 encoded hash</strong> (e.g. starts with <em>data:image/jpeg:base64./…..</em>)<strong>, </strong>but after many tests, I have found a workaround to this.</p><p>The workaround is to perform a click on the image box and save the src link from the image shown in the <strong>side panel</strong> instead of the thumbnails of the image. We must add time sleep function in order to have sufficient time to perform this action and this will mostly return direct URL links of the image sources.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dke6rb9Gr-Ki1Vkkt5EkBQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xg0UhMaI54yy8m8UYkzSaQ.png" /></figure><p>Here’s the snippet of the code:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/796eba639d1519603a5cad04671c1d3d/href">https://medium.com/media/796eba639d1519603a5cad04671c1d3d/href</a></iframe><p>Now we have a list of direct URLs to download the images in original resolution. However, a small percentage of the results will still return src with base64 encoding (as shown in the image below). We will have a try-catch to decode these later in the second script.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*R9nOYR1zbGk9kzLWCxKiSA.png" /></figure><p><strong>6</strong>. The next snippet calls the <strong>search_google</strong> function inside a for loop and appends the list of searched keywords and URLs of image source links onto a file called <strong>img_src_links.csv </strong>located in <strong>output/links:</strong></p><ul><li>using pipe (|) as the delimiter</li><li>replace whitespace with underscore (_) in search terms</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/877/1*qKQT1yu-xSLn5VLMgnIB5Q.png" /></figure><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3dcf59bdb67e06b7507293bfa7854755/href">https://medium.com/media/3dcf59bdb67e06b7507293bfa7854755/href</a></iframe><p>Here’s the complete first script (<a href="https://github.com/dianoctaviani/selenium_img_scraper/blob/main/selenium_img_src_crawler.py"><strong>selenium_img_src_crawler.py</strong></a>)</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4c239df20a18c55cb9ddb48e38ff4875/href">https://medium.com/media/4c239df20a18c55cb9ddb48e38ff4875/href</a></iframe><p>Now that we have the list of image source links, we will now process the links and download the series of images in the second script (<a href="https://github.com/dianoctaviani/selenium_img_scraper/blob/main/selenium_img_downloader.py"><strong>selenium_img_downloader.py</strong></a>)</p><ol><li><strong>Importing Essentials: </strong>The following modules are required for the second script.</li></ol><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1e2e60f115798f94b3428f0be501eab8/href">https://medium.com/media/1e2e60f115798f94b3428f0be501eab8/href</a></iframe><p><strong>2. </strong>The following snippet reads the list of image src that was retrieved from the previous script.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e36f12e37cc909f28cbef11a872dc31e/href">https://medium.com/media/e36f12e37cc909f28cbef11a872dc31e/href</a></iframe><p><strong>3. </strong>The following function called <strong>check_for_b64 </strong>is created to process the content of image src containing base64 encoding.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f891e113b65b3478975655e3a54406b4/href">https://medium.com/media/f891e113b65b3478975655e3a54406b4/href</a></iframe><p>4. I defined the socket default timeout to handle and terminate any long running requests.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a12fcd0777f82ec7b16c60e43b1c95f3/href">https://medium.com/media/a12fcd0777f82ec7b16c60e43b1c95f3/href</a></iframe><p>5. <strong>download_img()</strong> function had been written to process the request and perform downloads. It iterates through the list of src from <strong>img_src</strong> and identifies whether it is base64 src or direct URL. If it is base64 encoded hash, it will proceed to decode it else it will perform a web request to retrieve the image via image source URL.</p><p>It also dynamically assigns the name of the output file using the search terms and saves the image using the extension of <strong>.png</strong>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/637323de6786d9a612d95ed430b3ee0d/href">https://medium.com/media/637323de6786d9a612d95ed430b3ee0d/href</a></iframe><p>There may be some 403 Forbidden Errors that occur from the series of invoked web requests, and this is due to the unauthorised requests, which is unavoidable. The try-catch will skip any of these failed requests and logs the errors onto <strong>logging/logging.log.</strong></p><p>6. Check the <strong>output/images</strong> path to view the list of downloaded images, and we are done!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/718/1*lKlgB5J7EB80MuFvbfARnA.png" /></figure><p>Here’s the complete second script (<a href="https://github.com/dianoctaviani/selenium_img_scraper/blob/main/selenium_img_downloader.pyhttps://github.com/dianoctaviani/selenium_img_scraper/blob/main/selenium_img_downloader.py"><strong>selenium_img_downloader.py</strong></a>)</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5b3ab53c0498017a3538ad3b833e0e73/href">https://medium.com/media/5b3ab53c0498017a3538ad3b833e0e73/href</a></iframe><h3>Thank You for reading!</h3><blockquote>Code is available in: <a href="https://github.com/dianoctaviani/selenium_img_scraper">https://github.com/dianoctaviani/selenium_img_scraper</a></blockquote><p>Just clone it and install the required module, it should run straight away. Feel free to customise the input dataset and modify file paths to suit your needs! :)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3972ea3aa248" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>