Select a Subtopic
🚀 **Day 10: Avoiding Detection in Instagram Automation**
Instagram has **strict anti-bot policies**, so it's essential to make your bot behave more like a human. Today, you’ll learn techniques to:
- ✅ Use headless mode
- ✅ Modify user-agent
- ✅ Implement random delays
- ✅ Use proxies to rotate IPs
✅ **Step 1: Running in Headless Mode**
Running your bot in **headless mode** means the browser won't open a visible window. This reduces the chance of detection.
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
# Setup Chrome options
options = Options()
options.headless = True # Run in headless mode
options.add_argument("--disable-gpu") # Disable GPU to avoid issues
options.add_argument("--window-size=1920,1080") # Set window size
# Initialize the driver
service = Service("path/to/chromedriver")
driver = webdriver.Chrome(service=service, options=options)
# Open Instagram
driver.get("https://www.instagram.com/")
print("Instagram opened in headless mode.")
✅ **Step 2: Modifying the User-Agent**
Websites can detect bots based on the browser's **user-agent** string. Changing it makes your bot look more like a real user.
# Add a custom user-agent
options.add_argument("user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36")
✅ **Step 3: Random Delays to Mimic Human Behavior**
Adding **random delays** between actions makes your bot behave more like a human.
import time
import random
# Random delay function
def random_delay(min_time=2, max_time=5):
delay = random.uniform(min_time, max_time)
time.sleep(delay)
print(f"Delay for {delay:.2f} seconds.")
# Example usage
driver.get("https://www.instagram.com/")
random_delay()
✅ **Step 4: Using Proxies for IP Rotation**
Using **proxies** helps rotate your IP address to reduce detection.
# Use a proxy
options.add_argument("--proxy-server=http://your_proxy:port")
✅ **Practical Task for Day 10**
🛠️ Task:
- Run your bot in headless mode.
- Change the user-agent to mimic a real user.
- Implement random delays between actions.
- (Optional) Use a proxy if available.