【问题标题】:Followers in twitter (tweepy)推特上的追随者(tweepy)
【发布时间】:2018-03-21 21:02:23
【问题描述】:

我正在尝试下载我的 twitter 关注者和我的关注者的关注者。吨 该代码似乎有效,但它并没有下载我所有的追随者。它下载了一部分,在这部分我认为它运行良好。但为什么不是全部?

为什么会这样?

-- 编码:utf-8 --

"""
@author: Mik
"""
import csv 
import time 

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 = ''
api_secret = ''
access_token = '-'
access_token_secret = ''

# 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=''


#This creates a csv file and defines that each new entry will be in a new line 
csvfile=open(user+'network2.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) 

        #This is for private users that we wont be able to see their followers
        try:
            users=tweepy.Cursor(api.followers, screen_name = user, wait_on_rate_limit = True).items()
            for follower in users:
                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)

【问题讨论】:

    标签: python twitter tweepy


    【解决方案1】:

    如果我理解正确,那么您想获取每个关注者的所有关注者的 ID。 使用如下逻辑,它将每 15 分钟为您获取 3000 个关注者的详细信息

    import tweepy
    #twitter credentials here---------------------------------------------------
    auth = tweepy.OAuthHandler(your keys)
    auth.set_access_token(your keys)
    api = tweepy.API(auth)
    
    iter1 = tweepy.Cursor(api.followers, screen_name = 'your_screen_name',count = 200).pages()
    
    for request in range(15):
          your_200_followers = next(iter1)
          for each_follower in your_200_followers:
                   variable = each_follower.followers_ids
                   #store the <list> variable somewhere
    

    【讨论】:

      猜你喜欢
      • 2018-08-20
      • 2019-07-03
      • 1970-01-01
      • 1970-01-01
      • 2017-12-08
      • 2017-12-17
      • 2021-10-09
      • 1970-01-01
      • 2021-06-13
      相关资源
      最近更新 更多