Select a Subtopic
🚀 **Day 19: Advanced Instagram Bot Features (Selenium + Tkinter)**
Today, we'll build more powerful features for your Instagram bot:
- Automating Direct Messages (DMs).
- Scraping Instagram profile data (followers, following, posts).
- Improving GUI with status updates and error handling.
Let’s make your bot smarter and more interactive! 🎯
✅ **What You'll Learn Today:**
- Sending Automated DMs using Selenium.
- Scraping profile information from Instagram.
- Updating Tkinter GUI with live status updates.
- Handling errors and login issues.
📨 **1. Automating Direct Messages (DMs)**
We’ll create a function to send DMs to a specific user.
from selenium import webdriver from selenium.webdriver.common.by import By import time # Function to log in and send a DM def send_dm(username, password, recipient, message): driver = webdriver.Chrome(executable_path="path/to/chromedriver") driver.get("https://www.instagram.com/accounts/login/") time.sleep(5) # Login process driver.find_element(By.NAME, "username").send_keys(username) driver.find_element(By.NAME, "password").send_keys(password) driver.find_element(By.XPATH, '//*[@id="loginForm"]/div/div[3]/button').click() time.sleep(5) # Navigate to the recipient's profile driver.get(f"https://www.instagram.com/{recipient}/") time.sleep(5) # Click the message button driver.find_element(By.XPATH, "//button[contains(text(), 'Message')]").click() time.sleep(3) # Enter the message and send message_box = driver.find_element(By.XPATH, "//textarea") message_box.send_keys(message) driver.find_element(By.XPATH, "//button[contains(text(), 'Send')]").click() print(f"DM sent to {recipient}!") driver.quit()
🔍 **2. Scraping Instagram Profile Data**
We’ll create a function to scrape the number of followers, following, and posts from a profile.
# Function to scrape profile data def scrape_profile(username): driver = webdriver.Chrome(executable_path="path/to/chromedriver") driver.get(f"https://www.instagram.com/{username}/") time.sleep(5) # Get followers, following, and posts followers = driver.find_element(By.XPATH, "//li[1]/a/span").text following = driver.find_element(By.XPATH, "//li[2]/a/span").text posts = driver.find_element(By.XPATH, "//li[1]/span/span").text print(f"Profile: {username}") print(f"Followers: {followers}") print(f"Following: {following}") print(f"Posts: {posts}") driver.quit()
🖥 **3. Updating Tkinter GUI with Status Updates**
We’ll modify the Tkinter GUI to display status updates during the bot’s operation.
import tkinter as tk from selenium import webdriver from selenium.webdriver.common.by import By import time import threading # Function to update the status label def update_status(status_label, message): status_label.config(text=message) status_label.update_idletasks() # Function to start the bot def start_bot(username, password, recipient, message, status_label): try: driver = webdriver.Chrome(executable_path="path/to/chromedriver") update_status(status_label, "Logging in...") driver.get("https://www.instagram.com/accounts/login/") time.sleep(5) # Login process driver.find_element(By.NAME, "username").send_keys(username) driver.find_element(By.NAME, "password").send_keys(password) driver.find_element(By.XPATH, '//*[@id="loginForm"]/div/div[3]/button').click() time.sleep(5) # Navigate to the recipient's profile driver.get(f"https://www.instagram.com/{recipient}/") update_status(status_label, "Sending DM...") time.sleep(5) # Click the message button driver.find_element(By.XPATH, "//button[contains(text(), 'Message')]").click() time.sleep(3) # Enter the message and send message_box = driver.find_element(By.XPATH, "//textarea") message_box.send_keys(message) driver.find_element(By.XPATH, "//button[contains(text(), 'Send')]").click() update_status(status_label, f"DM sent to {recipient}!") driver.quit() except Exception as e: update_status(status_label, f"Error: {str(e)}") # GUI setup root = tk.Tk() root.title("Instagram DM Bot") root.geometry("400x300") username_label = tk.Label(root, text="Username:") username_label.grid(row=0, column=0, padx=10, pady=10) username_entry = tk.Entry(root) username_entry.grid(row=0, column=1, padx=10, pady=10) password_label = tk.Label(root, text="Password:") password_label.grid(row=1, column=0, padx=10, pady=10) password_entry = tk.Entry(root, show="*") password_entry.grid(row=1, column=1, padx=10, pady=10) recipient_label = tk.Label(root, text="Recipient:") recipient_label.grid(row=2, column=0, padx=10, pady=10) recipient_entry = tk.Entry(root) recipient_entry.grid(row=2, column=1, padx=10, pady=10) message_label = tk.Label(root, text="Message:") message_label.grid(row=3, column=0, padx=10, pady=10) message_entry = tk.Entry(root) message_entry.grid(row=3, column=1, padx=10, pady=10) status_label = tk.Label(root, text="Status: Idle") status_label.grid(row=5, column=0, columnspan=2, pady=20) start_button = tk.Button(root, text="Start Bot", command=lambda: threading.Thread(target=start_bot, args=(username_entry.get(), password_entry.get(), recipient_entry.get(), message_entry.get(), status_label)).start()) start_button.grid(row=4, column=0, columnspan=2, pady=10) root.mainloop()
🚀 **4. Handling Errors and Login Issues**
Let’s improve our bot to handle errors gracefully.
try: # Your Selenium code here except Exception as e: print(f"Error: {str(e)}")
🎯 **Day 19 Task:**
- Automate sending DMs.
- Scrape profile data (followers, following, posts).
- Update the GUI with live status.
- Handle errors in the bot.
Would you like to learn Day 20 next, where we’ll cover Instagram Story Viewing Automation and Cloud Deployment? 😊