39 – Real-World Python Projects – Instagram Auto Poster

⚠ Important Note

Instagram does NOT allow unofficial automated posting through normal login methods.
Using Selenium to log in may get accounts locked.

Safe Method: Use the official Instagram Graph API (requires a Business or Creator account).
Alternate Method (less safe): Selenium Web Automation.

I will give you both.


🟦 Method 1: Official Instagram API (Recommended + Safe)

Requires:

✔ Facebook Developer Account
✔ Instagram Business Account
✔ Connected Facebook Page
✔ Long-Lived Access Token
✔ Instagram Graph API permissions

Features

– Auto upload image
– Add caption
– Schedule posts
– Logs & analytics


📌 Step-by-step API Setup

I can generate the entire setup steps for you if you want, but here is the core posting script.


🧩 Python Code: Post Image Using Instagram Graph API

import requests
import json

INSTAGRAM_USER_ID = "YOUR_IG_USER_ID"
ACCESS_TOKEN = "YOUR_LONG_LIVED_TOKEN"

IMAGE_URL = "https://yourwebsite.com/post.jpg"
CAPTION = "Auto posted from Python 🚀 #python #automation"

# Step 1: Create media object
media_url = f"https://graph.facebook.com/v17.0/{INSTAGRAM_USER_ID}/media"
payload = {
    "image_url": IMAGE_URL,
    "caption": CAPTION,
    "access_token": ACCESS_TOKEN
}

media_response = requests.post(media_url, data=payload).json()
creation_id = media_response['id']

print("Media Created: ", creation_id)

# Step 2: Publish the media
publish_url = f"https://graph.facebook.com/v17.0/{INSTAGRAM_USER_ID}/media_publish"
publish_payload = {
    "creation_id": creation_id,
    "access_token": ACCESS_TOKEN
}

publish_response = requests.post(publish_url, data=publish_payload).json()
print("Post Published: ", publish_response)

✔ Supports captions
✔ No password required
✔ Official method → Instagram safe


🟧 Method 2: Selenium Instagram Auto Poster (Not API)

⚠ Risk: Account gets flagged if posting too often.


🧩 Python Selenium Auto Poster Code

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
import time

username = "your_username"
password = "your_password"
image_path = "photo.jpg"
caption = "Posted using Python 🚀"

driver = webdriver.Chrome()
driver.get("https://www.instagram.com/")

time.sleep(3)

# Login
driver.find_element(By.NAME, "username").send_keys(username)
driver.find_element(By.NAME, "password").send_keys(password + Keys.ENTER)

time.sleep(5)

# Click Create Post
driver.find_element(By.XPATH, "//div[text()='Create']").click()
time.sleep(3)

file_input = driver.find_element(By.XPATH, "//input[@accept='image/jpeg,image/png']")
file_input.send_keys(image_path)

time.sleep(3)

# Click Next buttons
driver.find_element(By.XPATH, "//button[text()='Next']").click()
time.sleep(2)

driver.find_element(By.XPATH, "//button[text()='Next']").click()
time.sleep(2)

# Add caption
driver.find_element(By.TAG_NAME, "textarea").send_keys(caption)
time.sleep(2)

# Share
driver.find_element(By.XPATH, "//button[text()='Share']").click()

time.sleep(5)
driver.quit()

🧭 Recommended Folder Structure

InstagramPoster/
│── auto_poster.py
│── post.jpg
│── config.json
│── schedule_post.py
│── requirements.txt

📦 requirements.txt

selenium
requests
python-dotenv
schedule

Install:

pip install -r requirements.txt


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *