Python Script: Download All Photos From Public Facebook Pages Easily

Looking for a simple Python Facebook photo downloader? This practical Python script automatically downloads all photos from any public Facebook page with minimal setup. The script uses Selenium to navigate through Facebook’s photo gallery, clicking into the lightbox and systematically downloading each image by simulating right-arrow key presses.

How the Python Facebook Photo Downloader Works

This script uses Selenium WebDriver to automate browser interactions. It opens the Facebook page’s photo section, clicks the first image to open the lightbox viewer, then systematically downloads each photo while navigating through the gallery.

Key Features of This Python Facebook Photo Downloader

  • Works with any public Facebook page
  • Automatically extracts image URLs and saves files locally
  • Handles Facebook’s dynamic content loading
  • Simple to customize for your specific needs

If the script gets stuck while trying to enter the lightbox view, you can manually click the first image and then let the script continue with the downloading loop.

The Python Facebook Photo Downloader Code

Here’s the complete Python script you can use to download Facebook photos. Make sure you have the Selenium package installed along with a compatible WebDriver for Firefox.

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By

import requests
import shutil
import re

# Initialize the Firefox WebDriver
driver = webdriver.Firefox()

# Navigate to the Facebook page's photos section
# Replace XXXXXXXXX with the page username or ID
driver.get("https://www.facebook.com/XXXXXXXXX/photos")

# Wait until the page title is loaded
element = WebDriverWait(driver, 10).until(
    EC.title_is("Ali's Magic Carpet Pre-Kindy - Photos | Facebook")
)

# Extract the image ID from the page source
html = driver.page_source
match = re.search(r'"id":"(\d*?)"},"media"', html).group(1)

# Wait for the element to be present
element = WebDriverWait(driver, 10).until(
    EC.presence_of_element_located((By.ID, match))
)

# Click on the first image to open the lightbox
driver.find_element_by_id(match).click()

# Loop through all photos in the gallery
while True:
    # Wait for the spotlight image to be loaded
    element = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.CLASS_NAME, "spotlight"))
    )
    
    # Get the image URL
    image_url = driver.find_element_by_class_name("spotlight").get_attribute("src")

    # Extract a filename from the URL
    filename = image_url.split("/")[-1]
    filename = filename.split("?")[0]
    
    # Download the image
    r = requests.get(image_url, stream=True)
    
    # Save the image if successfully retrieved
    if r.status_code == 200:
        r.raw.decode_content = True
        
        with open(filename, 'wb') as f:
            shutil.copyfileobj(r.raw, f)
            
        print('Image successfully Downloaded: ', filename)
    else:
        print('Image Couldn\'t be retrieved')
    
    # Move to the next image
    driver.find_element_by_css_selector('body').send_keys(Keys.RIGHT)

# Close the browser when done
driver.close()

Requirements for the Python Facebook Photo Downloader

Before running this script, make sure you have the following dependencies installed:

  • Python 3.6 or higher
  • Selenium WebDriver (pip install selenium)
  • Requests library (pip install requests)
  • Firefox browser and GeckoDriver

Customizing Your Python Facebook Photo Downloader

To use this script with a different Facebook page, replace the URL in the driver.get() line with your target page’s photos URL. You’ll also need to update the expected page title in the EC.title_is() condition.

Note: Facebook’s structure may change over time. If this script stops working, you might need to update the selectors or the approach used to identify and download images.

Legal Considerations When Using the Python Facebook Photo Downloader

Remember that this script should only be used with public Facebook pages where you have permission to download the content. Respect copyright and privacy laws when downloading and using images from social media platforms. According to Facebook’s Terms of Service, automated collection of data may be restricted.

Conclusion: Python Facebook Photo Downloader Made Simple

This Python Facebook photo downloader script offers a straightforward solution for bulk downloading images from public Facebook pages. It’s particularly useful for archiving content, creating backups, or collecting images for authorized use. With some basic Python knowledge, you can easily modify this script to suit your specific requirements.

Have you tried using Python for other social media automation tasks? Let me know in the comments below!