Automatically Find & Re-post Popular Instagram Content with Python

K. N
5 min readNov 13, 2020

Update (1/25/2021): It seems like the code no longer works, response is error 429 from instagram, which means too many requests. I will update it as soon as I can but until then, wanted to warn readers. I think having the code up might still help some and one can always make changes oneself so I am no taking the tutorial down for now but I will definitely get around to fixing it, thanks!

Disclaimer: Most of this code was obtained from other tutorials, I do not take credit for writing the selenium code. The purpose of this tutorial is to demonstrate Instagram automation and not to encourage posting/re-posting other people’s work. Unfortunately I am unable to find the original author of the code for Autogram, if anyone knows the author please do let me know so that I can give them credit for their awesome work.

The jupyter notebook containing all the code is available here.

What it does?

This takes a keyword as an input from the user and using it as a hashtag, retrieves public Instagram posts. It then sorts those posts based on the number of likes. The post with the most likes is then downloaded to be reposted later. It then pulls any hashtags from the caption, finds other hashtags being used with these hashtags on twitter and Instagram and use them in the caption, along with credit to the original poster of the selected post. The script then opens Instagram, logs into the user’s account and uploads the picture along with the caption.

Importing required libraries and set up the variables:

import requests
import urllib.request
import urllib.parse
import urllib.error
from bs4 import BeautifulSoup
import ssl
import json
from IPython.display import Image
import re
import time
import autoit
from selenium import webdriver
from import *
from selenium.webdriver.common.keys import Keys
import operator
import tweepy as tw
import bs4
import requests
consumer_key= ‘your-consumer-key’
consumer_secret= ‘your-consumer-secret’
access_token= ‘your-access-token’
access_token_secret= ‘your-access-token-secret’
auth = tw.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)