Issue
I am trying to extract a list of followers from a particular user using Tweepy. However, I ran into an error saying that AttributeError: 'API' object has no attribute 'followers_ids'
. This code, however, runs smoothly on another machine but not on mine. I tried to reinstall Tweepy but nothing changed. Please help T.T
import os
import sys
import json
import time
import math
from tweepy import Cursor
import tweepy
from tweepy import OAuthHandler
import datetime
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth, wait_on_rate_limit=True)
MAX_FRIENDS = 10000
def paginate(items, n):
"""Generate n-sized chunks from items"""
for i in range(0, len(items), n):
yield items[i:i+n]
def get_followers(screen_name):
# get followers for a given user
fname = "users/{}/followers.json".format(screen_name)
max_pages = math.ceil(MAX_FRIENDS / 5000)
with open(fname, 'w') as f:
for followers in Cursor(api.followers_ids, screen_name=screen_name).pages(max_pages):
for chunk in paginate(followers, 100):
users = api.lookup_users(user_ids=chunk)
for user in users:
f.write(json.dumps([user.id, user.screen_name, user.location, str(user.created_at)], sort_keys=True)+"\n")
if len(followers) == 5000:
print("More results available. Sleeping for 60 seconds to avoid rate limit")
time.sleep(60)
print("task completed for " + screen_name)
Solution
Tweepy v4.0.0 renamed API.followers_ids
to API.get_follower_ids
.
You're likely using an older version of Tweepy on the other machine.
Answered By - Harmon758
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.