Search code examples
pythontwittertweepy

Python script to follow Twitter user IDs


I am trying to create a little python script to follow Twitter user IDs from a textfile (one per line, in numeric format e.g. 217275660, 30921943, etc.). I took a look at this answer on stack exchange to make the code below using the 'try/except' answer, but I am getting an error "NameError: name 'TwitterError' is not defined"...

Anyone know how to clear this issue up and fix the code? I feel like it should be pretty simple but haven't used the Twitter API before.

# Script to follow Twitter users from text file containing user IDs (one per line)

# Header stuff I've just thrown in from another script to authenticate

import json
import time
import tweepy
import pprint
from tweepy.parsers import RawParser
from auth import TwitterAuth
from datetime import datetime

auth = tweepy.OAuthHandler(TwitterAuth.consumer_key, TwitterAuth.consumer_secret)

auth.set_access_token(TwitterAuth.access_token, TwitterAuth.access_token_secret)

rawParser = RawParser()

api = tweepy.API(auth_handler = auth, parser = rawParser)

# Follow everyone from list?!

with open('to_follow.txt') as f:
    for line in f:
        try:
            api.CreateFriendship(userID)
        except TwitterError:
            continue

print "Done."

Solution

  • That is may be because the tweepy throws error of type TweepError so you need to catch TweepError instead of TwitterError

    for line in f:
        try:
            api.CreateFriendship(userID)
        except TweepError,e:
            continue