Select a Subtopic
π **Day 11: Scrolling and Loading More Content with Selenium**
Instagram's feed and hashtag pages load content dynamically as you scroll. The goal today is to automate infinite scrolling and ensure new content loads.
ποΈ **Day 11 Focus: Automating Scrolling and Loading More Content**
Today, weβll focus on:
- β Using execute_script() to scroll.
- β Detecting when new content loads.
- β Interacting with dynamic elements after scrolling.
- β Automating the process to like posts as you scroll.
π» **Code Walkthrough: Scrolling on Instagram Hashtag Page**
π§© **Step 1: Set Up Your Environment**
Make sure you have Selenium installed and the Chrome WebDriver configured.
pip install selenium
π§© **Step 2: Basic Script to Open Instagram Hashtag Page**
Hereβs a starter script to open Instagram and scroll through a hashtag page.
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
import time
# Setup Chrome options
chrome_options = Options()
chrome_options.add_argument("--disable-notifications")
chrome_options.add_argument("--start-maximized")
# Initialize the driver
service = Service("path/to/chromedriver")
driver = webdriver.Chrome(service=service, options=chrome_options)
# Open Instagram and navigate to a hashtag page
driver.get("https://www.instagram.com/explore/tags/coding/")
# Wait for the page to load
time.sleep(5)
# Infinite scroll loop
for _ in range(5): # Adjust the range for more scrolling
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
time.sleep(2) # Adjust the sleep time based on your internet speedπ§© **Step 3: Detecting New Posts and Interacting with Them**
Letβs modify the script to like posts as new ones load.
# Find and like the first 10 posts
posts = driver.find_elements(By.CLASS_NAME, "_aagw") # Instagram post class
for index, post in enumerate(posts[:10]):
post.click()
time.sleep(2)
# Locate and click the like button
try:
like_button = driver.find_element(By.XPATH, "//span[@aria-label='Like']")
like_button.click()
print(f"Post {index + 1} liked!")
except:
print(f"Post {index + 1} was already liked.")
# Close the post
close_button = driver.find_element(By.XPATH, "//div[contains(@class,'_abl-')]")
close_button.click()
time.sleep(2)π§ͺ **Practical Task for Day 11**
β **Goal:**
- Scroll through the Instagram hashtag page.
- Like 10 new posts that load as you scroll.
β οΈ **Important Tips for Avoiding Detection**
- **Randomize your scrolling intervals:** Use
time.sleep()with random delays.import random time.sleep(random.uniform(2, 5))
- **Mimic Human Behavior:** Scroll slowly and avoid repetitive actions.