Web Scraping Iron Man Images Using Selenium and Python
Introduction to Web Scraping
Web scraping is the process of automatically extracting data and content from websites. Instead of manually copying information, web scraping uses bots to gather desired data from online sources quickly and efficiently. This technique is widely used for aggregating news, monitoring prices, collecting contact details, analyzing sentiment, building datasets for machine learning, and much more.
While web scraping is an incredibly powerful tool, it‘s important to be respectful when scraping websites. Overly aggressive scraping can put strain on web servers and some sites may have terms prohibiting scraping. As long as you limit your request rate and don‘t disrupt normal website operations, web scraping is generally acceptable for extracting publicly available information.
Selenium for Web Scraping
Selenium is a popular open-source tool for automating web browsers. Although primarily used for testing web applications, Selenium is also very handy for web scraping, especially when you need to interact with JavaScript-heavy websites. With the Selenium WebDriver library, you can programmatically launch a browser, navigate to pages, search for elements, extract data, fill forms, and more.
Python bindings for Selenium provide a convenient way to control a browser from Python code. Supported browsers include Chrome, Firefox, Safari, and Edge. Selenium is quite versatile and beginner-friendly, making it a great choice for web scraping projects.
Setting Up Selenium
Before we start scraping, let‘s get Selenium set up. First, make sure you have Python and pip installed. I recommend using Python 3.6+.
Next, install the selenium package:
pip install selenium
You‘ll also need to download the WebDriver for your browser of choice. For this example, we‘ll use Chrome. Check your Chrome version and download the corresponding ChromeDriver here:
https://chromedriver.chromium.org/downloads
Place the chromedriver executable in your working directory or add its location to your system PATH.
Finding Elements with CSS Selectors
One of the most crucial aspects of web scraping is locating the elements on the page that contain your desired data. Selenium offers multiple ways to find elements, including by ID, name, class, tag name, and XPath. However, one of the most flexible and powerful methods is using CSS selectors with the find_elements_by_css_selector function.
CSS selectors define patterns to match elements in an HTML document. They provide a way to select elements based on their tag, ID, class, attributes, and relationships to other elements. With CSS selectors, you can find elements in ways that would be cumbersome or impossible with other locator strategies.
For example, to find all <img> elements with a class of thumbnail, you would use:
thumbnail_images = driver.find_elements_by_css_selector("img.thumbnail")
This will return a list of all matching elements. You can then loop through the list to extract the src attribute containing the image URL:
for img in thumbnail_images:
img_url = img.get_attribute(‘src‘)
CSS selectors can get much more advanced. You can use combinators, pseudo-classes, and attribute selectors for precise targeting. For instance:
# Find all <a> elements that are direct children of <div> elements
# and have a class of "download-link"
download_links = driver.find_elements_by_css_selector("div > a.download-link")
As you can see, CSS selectors allow you to write concise, readable code to find exactly the elements you need. Spend time analyzing the structure of the pages you want to scrape and craft your selectors accordingly.
Scraping Iron Man Images
Now let‘s put this all together to scrape images of Iron Man from Google Images. Here‘s the full code:
import os
import time
import requests
from selenium import webdriver
def fetch_image_urls(query, max_images, wd, sleep_time):
search_url = "https://www.google.com/search?safe=off&site=&tbm=isch&source=hp&q={q}&oq={q}&gs_l=img"
wd.get(search_url.format(q=query))
image_urls = set()
image_count = 0
results_start = 0
while image_count < max_images:
# Scroll to the end of the page to load more image results
wd.execute_script("window.scrollTo(0, document.body.scrollHeight);")
time.sleep(sleep_time)
# Get all thumbnail image links
thumbnail_results = wd.find_elements_by_css_selector("img.Q4LuWd")
number_results = len(thumbnail_results)
print(f"Found: {number_results} search results. Extracting image links from {results_start}:{number_results}")
for img in thumbnail_results[results_start:number_results]:
try:
img.click()
time.sleep(sleep_time)
except Exception:
continue
# Extract actual image links
actual_images = wd.find_elements_by_css_selector(‘img.n3VNCb‘)
for actual_image in actual_images:
if actual_image.get_attribute(‘src‘) and ‘http‘ in actual_image.get_attribute(‘src‘):
image_urls.add(actual_image.get_attribute(‘src‘))
image_count = len(image_urls)
if image_count >= max_images:
print(f"Found: {image_count} image links, exiting...")
break
else:
print("Found:", image_count, "image links, continuing...")
time.sleep(30)
load_more_button = wd.find_element_by_css_selector(".mye4qd")
if load_more_button:
wd.execute_script("document.querySelector(‘.mye4qd‘).click();")
# Move result startpoint further down
results_start = len(thumbnail_results)
return image_urls
def save_image(folder_path, url, counter):
try:
image_content = requests.get(url).content
image_file = open(os.path.join(folder_path, ‘iron_man_‘ + str(counter) + ‘.jpg‘), ‘wb‘)
image_file.write(image_content)
image_file.close()
print(f"SUCCESS - saved {url} - as {folder_path}")
except Exception as e:
print(f"ERROR - Could not save {url} - {e}")
def search_images(search_term, num_images, path):
target_folder = os.path.join(path, ‘_‘.join(search_term.lower().split(‘ ‘)))
if not os.path.exists(target_folder):
os.makedirs(target_folder)
with webdriver.Chrome() as wd:
image_urls = fetch_image_urls(search_term, num_images, wd, 1)
counter = 0
for url in image_urls:
save_image(target_folder, url, counter)
counter += 1
search_images(‘iron man‘, 50, ‘../images/‘)
Let‘s break this down:
-
We import the necessary libraries – os for file/directory operations, time for adding delays, requests for downloading images, and webdriver from Selenium.
-
The
fetch_image_urlsfunction takes the search query, maximum number of images to scrape, WebDriver instance, and sleep time between requests. It navigates to the Google Images search URL, scrolls to the end of the page to load all results, and then uses CSS selectors to find thumbnail links and extract the actual image URLs from them. It keeps scrolling and loading more images until the desired number is reached. -
The
save_imagefunction simply downloads an image from its URL and saves it to the specified folder with an incrementing filename. -
The
search_imagesfunction is the main entry point. It creates the output folder if needed, launches a new Chrome browser with Selenium, callsfetch_image_urlsto get image URLs, and then saves each image usingsave_image. -
Finally, we call
search_imageswith the search term "iron man", a limit of 50 images, and the output path "../images/".
This script will launch a Chrome window, perform the image search, scroll through results, and save the first 50 Iron Man images it finds into the "../images/iron_man/" folder. The key aspects are using find_elements_by_css_selector to locate the thumbnail and full-size image elements, extracting URLs from the src attributes, and managing asynchronous page loading with strategic delays.
Considerations for Web Scraping
While this script successfully scrapes Iron Man images, there are a few things to keep in mind when web scraping in general:
- Respect website terms of service and robots.txt files that outline scraping permissions.
- Limit your request rate to avoid putting excessive load on web servers.
- Scrapers can break if the website layout changes, so periodically check your code.
- Many websites have anti-bot measures in place that can block scrapers. Selenium helps avoid some of these, but you may need more advanced techniques like IP rotation for large-scale scraping.
- Scraped data can be messy and may require cleaning before analysis or use in an application.
Web scraping is a powerful skill to have and opens up a whole world of possibilities for working with online data. With tools like Python, Selenium, and CSS selectors in your toolkit, you‘ll be able to extract all kinds of interesting information from websites.
Next Steps
Scraping images is just the beginning. You could easily adapt this code to scrape other types of content like text, links, tables, etc. Some interesting applications of web scraping include:
- Building datasets for machine learning projects (e.g. sentiment analysis, image classification)
- Monitoring prices of products across multiple sites
- Aggregating news articles or social media posts
- Generating leads for sales and marketing
- Analyzing trends and patterns over time
I hope this deep dive into web scraping with Python and Selenium has been informative and inspiring! Feel free to use and expand upon the code examples shown here for your own projects. Happy scraping!