The project outline:
Write a program that goes to a photo-sharing site like Flickr or Imgur, searches for a category of photos, and then downloads all the resulting images. You could write a program that works with any photo site that has a search feature.
My solution:
import sys, requests, os, bs4, time
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.firefox.service import Service
from selenium.webdriver.common.by import By
def get_image_posts(url):
# Returns a list of imgur post urls
res = requests.get(url)
res.raise_for_status()
soup = bs4.BeautifulSoup(res.text, "html.parser")
image_posts = ["https://imgur.com" + a["href"] for a in soup.select(".post a", href=True)]
if not image_posts:
print("Could not find any image posts.")
else:
return image_posts
def download_images(image_posts, driver, download_folder):
# Finds the image url in the post and downloads it.
for image_post in image_posts:
driver.get(image_post)
time.sleep(1)
content = driver.page_source.encode("utf-8").strip()
soup = bs4.BeautifulSoup(content, "html.parser")
image_elem = soup.select_one("img.image-placeholder")
if not image_elem:
print("Could not find an image.")
else:
image_url = image_elem["src"]
res = requests.get(image_url)
res.raise_for_status()
with open(os.path.join(download_folder, os.path.basename(image_url)), "wb") as image_file:
for chunk in res.iter_content(100000):
image_file.write(chunk)
def main():
search_term = sys.argv[1]
download_folder = sys.argv[2]
os.makedirs(download_folder, exist_ok=True)
service = Service(r"C:\Program Files\WebDrivers\geckdriver\geckodriver.exe")
options = Options()
options.binary_location = r"C:\Program Files\Mozilla Firefox\firefox.exe"
driver = webdriver.Firefox(service=service, options=options)
driver.get("https://imgur.com/")
try:
cookie_elem = driver.find_element(By. XPATH, "/html/body/div[1]/div/div/div/div[2]/div/button[2]")
search_elem = driver.find_element(By.CLASS_NAME, "Searchbar-textInput")
except:
print('Was not able to find an element with that name.')
cookie_elem.click()
search_elem.send_keys(search_term)
search_elem.submit()
time.sleep(1)
url = driver.current_url
image_posts = get_image_posts(url)
download_images(image_posts, driver, download_folder)
if __name__ == '__main__':
main()
I chose the site Imgur. My soluton works, though it is slow and clunky so I decided to limit it to one image download per page.