追随者网络与tweepy

时间:2018-03-12 20:42:00

标签: python csv twitter tweepy

我试图用Twitter和tweepy在Twitter上建立我的粉丝网络。我的问题是,我没有为每个用户获得所有关注者。这是代码:

import tweepy

# Copy the api key, the api secret, the access token and the access token secret from the relevant page on your Twitter app 

api_key = 'xxxx'
api_secret = 'xxxx'
access_token = 'x-x'
access_token_secret = 'xxxx'
# You don't need to make any changes below here # This bit authorises you to ask for information from Twitter 
auth = tweepy.OAuthHandler(api_key, api_secret) 
auth.set_access_token(access_token, access_token_secret) 
# The api object gives you access to all of the http calls that Twitter accepts 
api = tweepy.API(auth) 

#User we want to use as initial node 
user='xxxx'

import csv 
import time 
#This creates a csv file and defines that each new entry will be in a new line 
csvfile=open(user+'network.csv', 'w') 
spamwriter = csv.writer(csvfile, delimiter=' ',quotechar='|', quoting=csv.QUOTE_MINIMAL) 
#This is the function that takes a node (user) and looks for all its followers #and print them into a CSV file... and look for the followers of each follower... 

def fib(n,user,spamwriter):
    if n>0:
        #There is a limit to the traffic you can have with the API, so you need to wait 
        #a few seconds per call or after a few calls it will restrict your traffic 
        #for 15 minutes. This parameter can be tweeked 
        time.sleep(40) 
        try:
            users=api.followers(user) 
            for follower in users:
                print(follower.screen_name)
                spamwriter.writerow([user+';'+follower.screen_name]) 
                fib(n-1,follower.screen_name,spamwriter) 
                #n defines the level of autorecurrence

        except tweepy.TweepError:
            print("Failed to run the command on that user, Skipping...")

n=2
fib(n,user,spamwriter)

1 个答案:

答案 0 :(得分:1)

API.followers([id/screen_name])一次仅返回100个关注者。

尝试:

API.followers_ids(id/screen_name/user_id)

它将返回指定用户之后所有人的ID列表。只需将您的ID放入参数中即可。