Select a Subtopic
π Day 13: Handling Instagramβs Anti-Bot Measures
β 1. Using Proxies to Rotate IPs
A proxy server hides your IP address by acting as an intermediary between your bot and Instagram. This helps in avoiding detection, especially when you're making repeated requests.
from selenium import webdriver from selenium.webdriver.chrome.service import Service from selenium.webdriver.chrome.options import Options # Proxy setup PROXY = "123.45.67.89:8080" # Replace with a working proxy # Chrome options chrome_options = Options() chrome_options.add_argument(f'--proxy-server={PROXY}') # Initialize driver service = Service("path/to/chromedriver") driver = webdriver.Chrome(service=service, options=chrome_options) # Open Instagram driver.get("https://www.instagram.com/")
β 2. Randomizing Delays and Actions
Instagram can detect bots by analyzing the speed and pattern of interactions. To mimic human behavior:
- Use random delays between actions.
- Randomize the order of actions.
- Avoid repetitive tasks in quick succession.
import time import random # Function to add random delays def random_delay(): delay = random.uniform(2, 5) # Random delay between 2 to 5 seconds time.sleep(delay) # Example usage username.send_keys("your_username") random_delay() password.send_keys("your_password") random_delay() login_button.click()
β 3. Mimicking Human Behavior
To make your bot more human-like:
- Scroll the page gradually using
execute_script()
. - Hover over elements before clicking them.
- Interact with different sections of the page randomly.
# Scroll to the bottom of the page slowly for i in range(10): driver.execute_script("window.scrollBy(0, 500);") random_delay()
β 4. Complete Example Code
import time import random 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 # Proxy setup PROXY = "123.45.67.89:8080" # Chrome options chrome_options = Options() chrome_options.add_argument("--disable-notifications") chrome_options.add_argument("--start-maximized") chrome_options.add_argument(f'--proxy-server={PROXY}') # Initialize the driver service = Service("path/to/chromedriver") driver = webdriver.Chrome(service=service, options=chrome_options) # Open Instagram driver.get("https://www.instagram.com/") time.sleep(5) # Function to add random delays def random_delay(): delay = random.uniform(2, 5) time.sleep(delay) # Locate and interact with login fields username = driver.find_element(By.NAME, "username") password = driver.find_element(By.NAME, "password") login_button = driver.find_element(By.XPATH, "//button[@type='submit']") # Enter credentials with random delays username.send_keys("your_username") random_delay() password.send_keys("your_password") random_delay() login_button.click() # Mimic human scrolling behavior for i in range(10): driver.execute_script("window.scrollBy(0, 500);") random_delay() # Close the browser driver.quit()
π― Day 13 Practical Task
- β Set up proxy support in your bot.
- β Add random delays to your actions.
- β Implement scrolling behavior that mimics human activity.