Automatically Find & Re-post Popular Instagram Content with Python
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:
from bs4 import BeautifulSoup
from IPython.display import Image
from selenium import webdriver
from selenium.webdriver.chrome.options import *
from selenium.webdriver.common.keys import Keys
import operatorimport tweepy as tw
import requestsconsumer_key= ‘your-consumer-key’
auth = tw.OAuthHandler(consumer_key, consumer_secret)