Python 跟踪 Twitter 用户 ID 的脚本

Python script to follow Twitter user IDs

我正在尝试创建一个小 python 脚本来跟踪文本文件中的 Twitter 用户 ID(每行一个,采用数字格式,例如 217275660、30921943 等)。我查看了 this answer on stack exchange 以使用 'try/except' 答案制作下面的代码,但我收到错误 "NameError: name 'TwitterError' is not defined"...

有人知道如何解决这个问题并修复代码吗?感觉应该挺简单的 但是没用过推特API

# 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."

这可能是因为 tweepy 抛出 TweepError 类型的错误所以你需要捕获 TweepError 而不是 TwitterError

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