问题
Using Twython 3.1.0
trying to get followers of another user.
followers = twitter.get_followers_list(screen_name=user, count=500)
Even though user has 413 followers, I can only retrieve 200 of them.
Then I tried using Twyton's cursor
function:
followers = twitter.get_followers_list(screen_name=user, count=500, cursor=10)
It returns zero users:
len(followers(['users'])) = 0
On the bright side, I looked at How to get twitter followers using Twython? and I was able to get all followers id's with
followers = twitter.get_followers_ids(screen_name = "ryanmcgrath")
回答1:
I get the same thing, but a work around using the follower IDs would be something like this (assuming you just want to print the usernames):
user = raw_input("Enter Twitter handle to get followers of: ")
followids = twitter.get_followers_ids(screen_name = user)
for x in followids["ids"]:
data = twitter.show_user(user_id=x)
print(data["screen_name"])
Obviously you could do anything else you like with those screen names or user IDs (e.g. if using your own followers you could DM everyone the same message).
EDIT: The above example works, but it will run into rate limiting problems rapidly so a time delay to check IDs (or do other stuff) in batches would be wise.
EDIT 2: Your cursor code didn't work because the cursor count is the wrong format. The API documentation shows the sort of strings you can expect (e.g. "1374004777531007833"). The use of "count=N" is the number of results per page, which is why you hit the limit of 200 results. What you need to do is get each page, store the data somewhere and check for the "next_cursor" data in the results to get the next page until the value reaches 0.
EDIT 3: This works for me (without hitting the rate limits if the follower count is less than around 3,000) using Python 2.7.5 (for Python 3 you'll need to change the raw_input line):
import math
from twython import Twython
from authinfo import *
from config import *
twitter = Twython(APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
user = raw_input("Enter Twitter handle to get followers of: ")
suser = twitter.show_user(screen_name=user)
fnum = 200
pnum = int(math.ceil(float(suser["followers_count"]) / fnum))
pages = []
for i in range(pnum):
pages.append("p"+str(i+1))
oldpages = []
for i in range(pnum):
oldpages.append("p"+str(i))
p0 = { "next_cursor": -1 } # So the following exec() call doesn't fail.
for i in range(pnum):
exec(pages[i]+" = twitter.get_followers_list(screen_name=user, count=fnum, skip_status=1, cursor="+oldpages[i]+"['next_cursor'])")
followers = []
for p in range(pnum):
try:
exec("for i in range(fnum): followers.append("+pages[p]+"['users'][i])")
except(IndexError):
pass
print(len(followers))
for x in followers:
print("""Name: %s
Username: %s
""" % (x["name"], x["screen_name"]))
The authinfo.py module is where I keep the Twitter OAuth data (because it's static for my scripts) and the config.py module contains client_args variables. Ignore as necessary.
Some people (okay, probably a lot of people) don't like to encourage using exec(), but screw it, it works as a proof-of-concept. ;)
来源:https://stackoverflow.com/questions/19251084/twython-get-followers-list-can-only-get-200-followers