Select a Subtopic
🗓️ Day 14: Downloading Post Data (Images/Videos)
🚧 Step 1: Finding the Image URL
Instagram stores its images and videos in a specific URL format. We'll use Selenium to locate the URLs and extract the image's src attribute.
from selenium import webdriver
from selenium.webdriver.common.by import By
# Initialize the driver
driver = webdriver.Chrome("path/to/chromedriver")
driver.get("https://www.instagram.com/your_target_profile/")
# Wait for page to load
driver.implicitly_wait(10)
# Find the first post image
image = driver.find_element(By.XPATH, '//img[contains(@class, "FFVAD")]')
image_url = image.get_attribute("src")
print(f"Image URL: {image_url}")🚧 Step 2: Downloading the Image
Use the requests library to download the image and save it locally as a file.
import requests
# Download the image
response = requests.get(image_url)
# Save the image locally
with open("downloaded_image.jpg", "wb") as file:
file.write(response.content)
print("Image downloaded successfully!")🚧 Step 3: Downloading Multiple Images
Loop through multiple posts to download more images. This is helpful when scraping images from a profile or a hashtag page.
# Locate all images on the page
images = driver.find_elements(By.XPATH, '//img[contains(@class, "FFVAD")]')
# Loop through the images and download them
for index, img in enumerate(images):
img_url = img.get_attribute("src")
response = requests.get(img_url)
with open(f"image_{index + 1}.jpg", "wb") as file:
file.write(response.content)
print(f"Image {index + 1} downloaded!")🛠️ Practical Task for Day 14
- ✅ Scrape and download the first image from a profile.
- ✅ Create a loop to download the first 5 images from a hashtag page.
🚀 Next Steps for Day 15
Tomorrow, we'll start building a GUI for your bot using Tkinter! Ready to bring your bot to life with a desktop interface? 😃