Write a Twitter Bot with Python tweepy to Unfollow Non-Followers from the Command Line

Write a Twitter Bot with Python tweepy to Unfollow Non-Followers from the Command Line

What do you call those people who don’t follow you back on Twitter? Celebrities? News outlets? Using the Python tweepy library, we will write a bot to easily unfollow non-followers from the command line.

First, log in to http://dev.twitter.com and create a new application.

The create a file, keys.py , and fill it in with the appropriate information:

Now create a file, unfollow.py , and add the following(pun intended):

To run it from the command line:

You will be prompted to unfollow the non-followers you are currently following. Goodbye, parasites!

Let’s step through it.

The first few blocks should be familiar to you by now: we import tweepy, our keys, and connect to the Twitter API. In the next two blocks perform a similar role, we create lists, and populate them with followers and friends.

Next, we iterate over  friends  and use an if statement to check whether or not each element of friends is in followers . Before we destroy the friendship, we ask for verification.

It’s a simple matter of setting this up to run automatically with a cron job. That’s against Twitter’s Best Practices, so I’ll leave you to figure it out for yourself.

About the Author

Write a Twitter Bot with Python tweepy to Unfollow Non-Followers from the Command Line was posted by on . Jared is one half of the creative force behind Dototot. In addition to writing scripts and tutorials, he draws and animates both the digital and the analog.

Discuss this article in the Comments below, or and it with your friends. Learn more about our creative process by subscribing to the New Moon Newsletter .

Recommended for you
Join the discussion
  • James Kan

    Does anyone know if this has a chance of getting you banned? Since it is against Twitters ToS to follow/unfollow too many people at once.

  • لزيادة متابعينك

    is this true?? by the way thank you Jared

  • Tw33tingOwl

    You missed a comma out in keys on screen name. I forgot it was a dictionary list and was scratching my head for a while because kept erroring out. I added the coma at end of screen name it worked a treat.

  • Sean D. Mack

    Hmmm, doesn’t seem to be working for larger than 5000. The list of followers and friends only appears to be 5000 long. Any advice on how to handle? Thanks!

  • Jerry Yates

    Using tweepy’s cursor object should be able to handle it but I’m still getting hit with rate limit after about 300. Maybe you can sort it.

  • Sean D. Mack

    Yes, been playing with this as well on a completely separate script. https://gist.github.com/jamiesonbecker/1243373

    But that’s taking hours and timing out! :)

Subscribe to our New Moon Newsletter

How do you make money doing what you love? Sign up for monthly emails exploring the necessary evils of business as we develop a creative media studio using free and open source software.

The email entered is invalid